PostgreSQL Table Operations
This guide covers common table operations in PostgreSQL, including adding columns, modifying table structure, and other essential database management tasks.
Table of Contents
- Adding Columns
- Removing Columns
- Modifying Columns
- Renaming Columns
- Adding Constraints
- Removing Constraints
- Renaming Tables
- Truncating Tables
Adding Columns
To add a new column to an existing table, use the ALTER TABLE statement with the ADD COLUMN clause.
Syntax
ALTER TABLE table_nameADD COLUMN column_name data_type [constraints];Examples
Add a simple column without constraints:
ALTER TABLE employeesADD COLUMN email VARCHAR(255);Add a column with constraints:
ALTER TABLE employeesADD COLUMN age INTEGER CHECK (age >= 18);Add a column with a default value:
ALTER TABLE employeesADD COLUMN created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP;Add multiple columns at once:
ALTER TABLE employeesADD COLUMN phone VARCHAR(20),ADD COLUMN department VARCHAR(100);Removing Columns
To remove a column from a table, use the ALTER TABLE statement with the DROP COLUMN clause.
Syntax
ALTER TABLE table_nameDROP COLUMN column_name;Examples
Remove a single column:
ALTER TABLE employeesDROP COLUMN email;Remove multiple columns:
ALTER TABLE employeesDROP COLUMN phone,DROP COLUMN department;Note: Dropping a column will permanently remove all data in that column.
Modifying Columns
To change the data type or constraints of an existing column, use the ALTER COLUMN clause.
Syntax
ALTER TABLE table_nameALTER COLUMN column_name TYPE new_data_type;Examples
Change the data type of a column:
ALTER TABLE employeesALTER COLUMN age TYPE SMALLINT;Set a default value for a column:
ALTER TABLE employeesALTER COLUMN department SET DEFAULT 'HR';Remove the default value from a column:
ALTER TABLE employeesALTER COLUMN department DROP DEFAULT;Set a column to NOT NULL:
ALTER TABLE employeesALTER COLUMN email SET NOT NULL;Remove NOT NULL constraint from a column:
ALTER TABLE employeesALTER COLUMN email DROP NOT NULL;Renaming Columns
To rename a column, use the RENAME COLUMN clause.
Syntax
ALTER TABLE table_nameRENAME COLUMN old_name TO new_name;Example
ALTER TABLE employeesRENAME COLUMN created_at TO hire_date;Adding Constraints
You can add constraints to existing columns using the ADD CONSTRAINT clause.
Syntax
ALTER TABLE table_nameADD CONSTRAINT constraint_name constraint_definition;Examples
Add a primary key constraint:
ALTER TABLE employeesADD CONSTRAINT pk_employees_id PRIMARY KEY (id);Add a unique constraint:
ALTER TABLE employeesADD CONSTRAINT uk_employees_email UNIQUE (email);Add a foreign key constraint:
ALTER TABLE employeesADD CONSTRAINT fk_employees_departmentFOREIGN KEY (department_id) REFERENCES departments(id);Add a check constraint:
ALTER TABLE employeesADD CONSTRAINT chk_employees_age CHECK (age >= 18);Removing Constraints
To remove constraints, use the DROP CONSTRAINT clause.
Syntax
ALTER TABLE table_nameDROP CONSTRAINT constraint_name;Examples
Remove a named constraint:
ALTER TABLE employeesDROP CONSTRAINT uk_employees_email;Remove a primary key constraint:
ALTER TABLE employeesDROP CONSTRAINT pk_employees_id;Note: If you don’t know the constraint name, you can find it by querying the information schema or using
pg_constraintsystem table.
Renaming Tables
To rename a table, use the RENAME TO clause.
Syntax
ALTER TABLE old_table_nameRENAME TO new_table_name;Example
ALTER TABLE employeesRENAME TO staff;Truncating Tables
To remove all rows from a table while keeping the table structure, use the TRUNCATE statement.
Syntax
TRUNCATE TABLE table_name;Example
TRUNCATE TABLE employees;Note:
TRUNCATEis faster thanDELETEwithout a WHERE clause, but it cannot be rolled back in some database systems and does not trigger ON DELETE triggers.
Best Practices
- Always backup your database before making structural changes
- Test ALTER TABLE operations on a development database first
- Consider the impact on indexes, constraints, and foreign keys when modifying tables
- Use transactions for critical table modifications when possible
- Document all structural changes to your database schema