The ALTER TABLE command in Post­greSQL lets you add or modify columns in database tables.

What is Post­greSQL’s ALTER TABLE?

The ALTER TABLE command in Post­greSQL can be used to modify tables in a database. This command lets you add, remove or adjust columns in a table as needed. It can also be used to implement or lift con­straints on a table in the database man­age­ment system. To use this command, you need to combine it with a specific action.

Dedicated Servers
Per­for­mance through in­no­va­tion
  • Dedicated en­ter­prise hardware
  • Con­fig­urable hardware equipment
  • ISO-certified data centers

What is the syntax for ALTER TABLE?

To get a better un­der­stand­ing of how to use ALTER TABLE, let’s first take a look at its syntax:

ALTER TABLE table_name action;
post­gresql

After the command, you need to specify the name of the table you want to modify and choose the cor­re­spond­ing action for the changes you want to make.

Tip

To create a new table, you can use the CREATE TABLE command in Post­greSQL.

Post­greSQL ALTER TABLE examples

Below, we’ll demon­strate how ALTER TABLE works with a simple example. We’ll use a table named customers that has three columns and three rows:

id name city
1 Lee New York
2 Johnson Los Angeles
3 Vargas Chicago

We can adjust this table in various ways using ALTER TABLE.

Adding a column with Post­greSQL ADD COLUMN

To add a new column, use ALTER TABLE in com­bi­na­tion with Post­greSQL’s ADD COLUMN action. This action requires two pa­ra­me­ters: the name of the new column and its data type. The syntax is as follows:

ALTER TABLE table_name ADD COLUMN column_name data_type;
post­gresql

For example, here’s how you can add an address column to the customers table:

ALTER TABLE customers ADD COLUMN address VARCHAR(255);
post­gresql

Here’s what the table looks like now:

id name city address
1 Lee New York NULL
2 Johnson Los Angeles NULL
3 Vargas Chicago NULL

Removing a column with DROP COLUMN

To remove a column from a table, use ALTER TABLE with the DROP COLUMN action. Here, you only need to add the column name as a parameter:

ALTER TABLE table_name DROP COLUMN column_name;
post­gresql

To remove the city column, use the following code:

ALTER TABLE customers DROP COLUMN city;
post­gresql

This reduces the table to three columns:

id name address
1 Lee NULL
2 Johnson NULL
3 Vargas NULL

Renaming a column with RENAME COLUMN

You can also rename an existing column. This can be a good al­ter­na­tive to deleting columns and then adding them again. The syntax for RENAME COLUMN is:

ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name;
post­gresql

For example, you can change the name column to customer_name:

ALTER TABLE customers RENAME COLUMN name TO customer_name;
post­gresql

Here’s what the table looks like now:

id customer_name address
1 Lee NULL
2 Johnson NULL
3 Vargas NULL

Ad­di­tion­al Post­greSQL actions for ALTER TABLE

Here are some other key actions you can use with ALTER TABLE:

Changing the data type of a column:

ALTER TABLE table_name ALTER COLUMN column_name TYPE data_type;
post­gresql

Making sure that every entry in a column has a value:

ALTER TABLE table_name ALTER COLUMN column_name SET NOT NULL;
post­gresql

Es­tab­lish­ing con­straints such as UNIQUE or PRIMARY KEY by using ALTER TABLE with ADD CONSTRAINT:

ALTER TABLE table_name ADD CONSTRAINT constraint_name constraint_definition;
post­gresql
Go to Main Menu