Adding Columns to Tables in PostgreSQL

Suppose we have a table named employees with these columns:

ColumnData Type
idSERIAL PRIMARY KEY
nameVARCHAR(100)
emailVARCHAR(100)
hire_dateDATE

To add new columns, use the syntax:

ALTER TABLE employees
ADD COLUMN column_name data_type [constraints];

Important: The ALTER TABLE ... ADD COLUMN command does not produce output. It only modifies the table structure.

Example 1: Add a phone number column

ALTER TABLE employees
ADD COLUMN phone VARCHAR(15);

After this, the employees table will look like:

ColumnData Type
idSERIAL PRIMARY KEY
nameVARCHAR(100)
emailVARCHAR(100)
hire_dateDATE
phoneVARCHAR(15)

Example 2: Add a status column with constraints and default

ALTER TABLE employees
ADD COLUMN status VARCHAR(20) NOT NULL DEFAULT 'active';

After this, the employees table will look like:

ColumnData TypeConstraints
idSERIAL PRIMARY KEY
nameVARCHAR(100)
emailVARCHAR(100)
hire_dateDATE
phoneVARCHAR(15)
statusVARCHAR(20)NOT NULL DEFAULT ‘active’

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top