Site icon JD Bots

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

Syntax

ALTER TABLE table_name
    ADD COLUMN [ IF NOT EXISTS ] column_name data_type [ DEFAULT value ];

See ALTER TABLE 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=#

CREATE DATABASE cinema;

Output

CREATE DATABASE

postgres=#

\c cinema

Output

You are now connected to database "cinema" as user "postgres".

Step 2: Create the Table and Add Two Films

cinema=#

CREATE TABLE films (title text);

Output

CREATE TABLE

cinema=#

INSERT INTO films (title) VALUES ('The Lantern Keeper');

Output

INSERT 0 1

cinema=#

INSERT INTO films (title) VALUES ('Ocean of Kites');

Output

INSERT 0 1

Step 3: Add a Column

cinema=#

ALTER TABLE films ADD COLUMN year integer;

Output

ALTER TABLE

Here’s the table now:

cinema=#

SELECT * FROM films;

Output

       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=#

ALTER TABLE films ADD COLUMN rating text DEFAULT 'PG';

Output

ALTER TABLE

cinema=#

SELECT * FROM films;

Output

       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=#

ALTER TABLE films ADD COLUMN year integer;

Output

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=#

ALTER TABLE films ADD COLUMN IF NOT EXISTS year integer;

Output

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=#

\c postgres

Output

You are now connected to database "postgres" as user "postgres".

postgres=#

DROP DATABASE cinema;

Output

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

Related

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

Exit mobile version