Skip to main content
0 reactions

How to Set a Default Value for a Column in PostgreSQL

Featured card for How to Set a Default Value for a Column in PostgreSQL, with a terminal window listing DEFAULT ‘waiting’, INSERT (‘Box 1’), status: waiting and VALUES (…, DEFAULT).
How to set a default value for a column in PostgreSQL.

A default value fills a column when an INSERT doesn’t give one. In PostgreSQL, add DEFAULT and the value after the column’s type. Here’s how to set a default value for a column, with a small parcel tracker.

Every command and output on this page is real, from PostgreSQL 19 on macOS.

Syntax

SyntaxSQL
column_name data_type DEFAULT value
  • DEFAULT value: the value a new row gets when the insert leaves the column out.
  • In an INSERT, the key word DEFAULT in place of a value asks for the default too.
  • Without DEFAULT, a column’s default is NULL: empty.

See Default Values (opens in a new tab) in the PostgreSQL documentation.

The Example

A parcel tracker where every new parcel starts as waiting.

Step 1: Create a Test Database

Open Terminal and connect to your PostgreSQL server with psql as the postgres user:

TerminalBash
psql -U postgres

Type the user’s password when psql asks for it. psql connects you to the postgres database, and its prompt is postgres=#.

Create a database for the example and connect to it:

postgres=#SQL
CREATE DATABASE deliveries;
OutputPlain text
CREATE DATABASE
postgres=#Plain text
\c deliveries
OutputPlain text
You are now connected to database "deliveries" as user "postgres".

Step 2: Create a Table with a Default

deliveries=#SQL
CREATE TABLE parcels (label text, status text DEFAULT 'waiting');
OutputPlain text
CREATE TABLE

Step 3: Insert Rows

Leave the status out:

deliveries=#SQL
INSERT INTO parcels (label) VALUES ('Box 1');
OutputPlain text
INSERT 0 1

Give it a value:

deliveries=#SQL
INSERT INTO parcels (label, status) VALUES ('Box 2', 'sent');
OutputPlain text
INSERT 0 1

Ask for the default with the key word:

deliveries=#SQL
INSERT INTO parcels (label, status) VALUES ('Box 3', DEFAULT);
OutputPlain text
INSERT 0 1
deliveries=#SQL
SELECT * FROM parcels;
OutputPlain text
 label | status
-------+---------
 Box 1 | waiting
 Box 2 | sent
 Box 3 | waiting
(3 rows)

Box 1 and Box 3 got waiting; Box 2 kept the value it was given.

Step 4: See the Default

deliveries=#Plain text
\d parcels
OutputPlain text
                 Table "public.parcels"
 Column | Type | Collation | Nullable |     Default
--------+------+-----------+----------+-----------------
 label  | text |           |          |
 status | text |           |          | 'waiting'::text

The Default column shows 'waiting'::text: the text waiting, as a text value.

Clean Up

To remove the example, connect to another database and drop the test database:

deliveries=#Plain text
\c postgres
OutputPlain text
You are now connected to database "postgres" as user "postgres".
postgres=#SQL
DROP DATABASE deliveries;
OutputPlain text
DROP DATABASE

Warning: DROP DATABASE deletes the database and everything in it, and it can’t be undone.

Common Mistakes

MistakeWhat happensDo this instead
Expecting a default for a column the insert namesA value you give always wins, as Box 2 showed.Leave the column out, or write DEFAULT in its place.

FAQ

Can a default be worked out when the row arrives?

Yes. A default can be an expression, which PostgreSQL works out for each new row, such as the current time.

Conclusion

Add DEFAULT and a value to a column, and every insert that leaves the column out gets that value.

Additional Resources

Postgres, PostgreSQL and the Slonik Logo are trademarks or registered trademarks of the PostgreSQL Community Association of Canada, and used with their permission.

0 comments