Skip to main content
0 reactions

How to Add a Column to a Table in PostgreSQL

Featured card for How to Add a Column to a Table in PostgreSQL, with a terminal showing ALTER TABLE films, ADD COLUMN year, existing rows getting NULL, or the DEFAULT PG.
How to Add a Column to a Table in PostgreSQL

To add a column to a table in PostgreSQL, use ALTER TABLE … ADD COLUMN with the new column’s name and type. The rows already in the table keep their data, and the new column is empty for them, or gets a default you give. Here’s how, with a small example.

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

Syntax

SyntaxSQL
ALTER TABLE table_name
    ADD COLUMN [ IF NOT EXISTS ] column_name data_type [ DEFAULT value ];
  • table_name: the table that gets the new column.
  • column_name data_type: the new column’s name and type, as in CREATE TABLE.
  • DEFAULT value: optional. The value every existing row gets, and every new row that doesn’t give one. Without it, existing rows get NULL, an empty value.
  • IF NOT EXISTS: optional. If the column is already there, skip it with a notice instead of an error.
  • [ ] marks the optional parts.

See ALTER TABLE (opens in a new tab) in the PostgreSQL documentation.

The Example

A cinema keeps its films in a table that has only a title. You add the year each film came out, and an age rating that starts as PG.

Step 1: Create a Test Database

Connect to your server with psql as the postgres user, as Part 2 of the PostgreSQL Tutorial shows. 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 cinema;
OutputPlain text
CREATE DATABASE
postgres=#Plain text
\c cinema
OutputPlain text
You are now connected to database "cinema" as user "postgres".

Step 2: Create the Table and Add Two Films

cinema=#SQL
CREATE TABLE films (title text);
OutputPlain text
CREATE TABLE
cinema=#SQL
INSERT INTO films (title) VALUES ('The Lantern Keeper');
OutputPlain text
INSERT 0 1
cinema=#SQL
INSERT INTO films (title) VALUES ('Ocean of Kites');
OutputPlain text
INSERT 0 1

Step 3: Add a Column

cinema=#SQL
ALTER TABLE films ADD COLUMN year integer;
OutputPlain text
ALTER TABLE

Here’s the table now:

cinema=#SQL
SELECT * FROM films;
OutputPlain text
       title        | year
--------------------+------
 The Lantern Keeper |
 Ocean of Kites     |
(2 rows)

Both films are still there, and their year is empty (NULL): nothing gave them a year.

Step 4: Add a Column with a Default

cinema=#SQL
ALTER TABLE films ADD COLUMN rating text DEFAULT 'PG';
OutputPlain text
ALTER TABLE
cinema=#SQL
SELECT * FROM films;
OutputPlain text
       title        | year | rating
--------------------+------+--------
 The Lantern Keeper |      | PG
 Ocean of Kites     |      | PG
(2 rows)

Every existing film got the default, PG, and a new film gets it too when it doesn’t give a rating. PostgreSQL stores a default like this without rewriting the table, so the change is fast even on a big table.

Step 5: Add a Column Only If It’s Missing

Adding year a second time fails, because the column exists:

cinema=#SQL
ALTER TABLE films ADD COLUMN year integer;
OutputPlain text
ERROR:  column "year" of relation "films" already exists

IF NOT EXISTS turns the error into a notice, which helps in a script that may run twice:

cinema=#SQL
ALTER TABLE films ADD COLUMN IF NOT EXISTS year integer;
OutputPlain text
NOTICE:  column "year" of relation "films" already exists, skipping
ALTER TABLE

Clean Up

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

cinema=#Plain text
\c postgres
OutputPlain text
You are now connected to database "postgres" as user "postgres".
postgres=#SQL
DROP DATABASE cinema;
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
Adding a column that’s already thereERROR: column "year" of relation "films" already existsChoose another name, or use ADD COLUMN IF NOT EXISTS.
Expecting existing rows to get a value without a defaultThey get NULL, an empty value, as year did.Give the column a DEFAULT when existing rows need a value.

FAQ

Can I add a column at a certain position?

No. ADD COLUMN has no option for a position: a new column always goes at the end, as year and rating did. To see the columns in another order, list them in that order in your SELECT.

Conclusion

ALTER TABLE … ADD COLUMN adds a column to the end of a table. Existing rows get NULL, or the DEFAULT you give, and IF NOT EXISTS skips a column that’s already there.

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