PostgreSQL – Delete Data from a Table

To add a new column to a table in PostgreSQL, you can use the ALTER TABLE statement with the ADD COLUMN clause. Syntax is as shown below:

ALTER TABLE table_name
ADD COLUMN column_name data_type [column_constraints];

Where:

  • table_name is the name of the table to which you want to add a new column.
  • column_name is the name of the new column you want to add.
  • data_type is the data type of the new column.
  • column_constraints (optional) are any additional constraints you would like to apply to the new column, such as NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, etc.

Below is an example to add a new column “email” of data type varchar(255) to a table “users”.

ALTER TABLE users
ADD COLUMN email varchar(255);

If you want to add a constraint to the new column, you can include it in the statement. For instance, if you want to add unique constraint to the email column , you can use the following SQL statement:

ALTER TABLE users
ADD COLUMN email varchar(255) UNIQUE;

Similar Posts