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
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 (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:
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:
CREATE DATABASE deliveries;
CREATE DATABASE
\c deliveries
You are now connected to database "deliveries" as user "postgres".
Step 2: Create a Table with a Default
CREATE TABLE parcels (label text, status text DEFAULT 'waiting');
CREATE TABLE
Step 3: Insert Rows
Leave the status out:
INSERT INTO parcels (label) VALUES ('Box 1');
INSERT 0 1
Give it a value:
INSERT INTO parcels (label, status) VALUES ('Box 2', 'sent');
INSERT 0 1
Ask for the default with the key word:
INSERT INTO parcels (label, status) VALUES ('Box 3', DEFAULT);
INSERT 0 1
SELECT * FROM parcels;
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
\d parcels
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:
\c postgres
You are now connected to database "postgres" as user "postgres".
DROP DATABASE deliveries;
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
- PostgreSQL 19 documentation: Default Values (opens in a new tab)
- PostgreSQL 19 documentation: CREATE TABLE (opens in a new tab)
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