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
Syntax
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 wordDEFAULTin place of a value asks for the default too. - Without
DEFAULT, a column’s default is NULL: empty.
See Default Values 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:
Terminal
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=#
CREATE DATABASE deliveries;
Output
CREATE DATABASE
postgres=#
\c deliveries
Output
You are now connected to database "deliveries" as user "postgres".
Step 2: Create a Table with a Default
deliveries=#
CREATE TABLE parcels (label text, status text DEFAULT 'waiting');
Output
CREATE TABLE
Step 3: Insert Rows
Leave the status out:
deliveries=#
INSERT INTO parcels (label) VALUES ('Box 1');
Output
INSERT 0 1
Give it a value:
deliveries=#
INSERT INTO parcels (label, status) VALUES ('Box 2', 'sent');
Output
INSERT 0 1
Ask for the default with the key word:
deliveries=#
INSERT INTO parcels (label, status) VALUES ('Box 3', DEFAULT);
Output
INSERT 0 1
deliveries=#
SELECT * FROM parcels;
Output
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=#
\d parcels
Output
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=#
\c postgres
Output
You are now connected to database "postgres" as user "postgres".
postgres=#
DROP DATABASE deliveries;
Output
DROP DATABASE
Warning: DROP DATABASE deletes the database and everything in it, and it can’t be undone.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Expecting a default for a column the insert names | A 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.
