Introduction to PostgreSQL ALTER TABLE
PostgreSQL alter table statement is used to change the table structure, we can change the table name, column name, add or drop the table column, add or drop the constraint from the table, set new schema to the table. To use alter table statement in PostgreSQL we need to have the owner of table privileges or superuser privileges to execute the alter table statement. Alter table statement is used to delete, add, and modify the structure of the existing table in PostgreSQL.
Syntax of PostgreSQL ALTER TABLE
Below is the syntax :
1. Alter table to change the name of the table
Alter table name_of_table RENAME TO new_name_of_table;
2. Alter table to add a column
Alter table name_of_table ADD name_of_column data_type_of_column;
3. Alter table to drop column
Alter table name_of_table DROP name_of_column;
4. Alter table to change the data type of column
Alter table name_of_tableALTER COLUMN name_of_column type data_type_of_column;
5. Alter table to set new schema
Alter table name_of_table SET SCHEMA new_schema_name;
6. Alter table to rename the column
Alter table name_of_table rename column old_column_name to new_column_name;
7. Alter column to add a not-null constraint
Alter table name_of_table modify name_of_column data_type not null;
8. Alter table to drop constraint
Alter table name_of_table drop constraint name_of_constraint;
9. Alter table to change the tablespace
Alter table name_of_table set tablespace name_of_tablespace;
Parameters
Below is the parameter description syntax of the alter table statement in PostgreSQL.
- Alter table –This statement is used to change the table structure in PostgreSQL.
- Name of the table –This is defined as the table name which was we have used to change the structure of the table.
- Rename to – This keyword is used to change the table name, we have change table name using rename keyword.
- The new name of the table –This is the newly created name of the table which was we have modified with alter table statement.
- Add column –We can add a column of the table by using add column keyword with alter table statement.
- Column name –This is defined as the name of the column which was we have changed with alter table statement.
- The data type of column –This is defined as data type which was we have allocated to the column.
- Modify –This keyword is used to modify the column to add not null constraint in PostgreSQL.
- Set tablespace –This is used to set the new tablespace to the table by using the alter table statement.
- Set schema – This is used to set the new schema to the table by using the alter table statement.
- Drop constraint –This is used to drop constraint from the table using the alter table statement.
How to ALTER TABLE statement work in PostgreSQL?
Below is the working of the alter table statement in PostgreSQL. Alter table in PostgreSQL is used to change the structure of the table.
The below example shows that we need to have privileges of the owner of the table or superuser privileges.
psql -U db_test -d testing
ALTER TABLE student DROP stud_id;
psql -U Postgres
\c testing
ALTER TABLE student DROP stud_id;
\d+ student;
In the above first example, we have used user as db_test, this user doesn’t have privileges of table owner or superuser so, it will issue error while executing the alter table statement.
In the second example, we have altered the table using the username as Postgres, after using this user we have to drop the column name of stud_id from the student table.
Alter table is used to do the following action on the table are as follows.
- Change the name of the table.
- Add column.
- Drop column.
- Change the schema of the table.
- Change the tablespace of the table.
- Add a constraint to the table.
- Drop constraint from the table.
- Rename column.
- Add not null constraint.
Examples
Below is the example of the alter table statement in PostgreSQL are as follows. We have using stud1 and stud2 tables to describe examples of the alter table statement.
Below is the table description of stud1 and stud2 table.
\d+ stud1;
\d+ stud2;
Example #1 – Alter table to change the name of the table
Below example shows that alter table to change the name of the table. We have to change the stud1 table name to student1.
Alter table stud1 RENAME TO student1;
\d+ student1;
Example #2 – Alter table to add a new column
Below example shows that alter table to add the new column. We have added the test_stud column to the student1 table.
Alter table student1 ADD test_stud varchar;
\d+ student1;
Example #3 – Alter table to drop column
Below example shows that alter table to drop the column. We have dropped the test_stud column from the student1 table.
Alter table student1 DROP COLUMN test_stud;
\d+ student1;
Example #4 – Alter table to change the schema of the table
The below example shows that change the schema of the stud2 table. We have changed the schema public to my_schema.
Alter table stud2 SET SCHEMA my_schema;
\d+ my_schema.stud2;
Example #5 – Alter table to change tablespace of a table
The below example shows that change the tablespace of the stud2 table. We have changed tablespace from default to my_test.
Alter table my_schema.stud2 set tablespace my_test;
\d+ my_schema.stud2;
Example #6 – Alter table to rename the column
Below example shows that alter table to rename the column. We have to change the id column to the id1 column.
Alter table my_schema.stud2 rename column id to id1;
\d+ my_schema.stud2;
Recommended Articles
This is a guide to PostgreSQL ALTER TABLE. Here we discuss how to ALTER TABLE in PostgreSQL along with query examples. You may also have a look at the following articles to learn more –