Introduction to Alter Column in PostgreSQL
Alter column in PostgreSQL used to change the table column structure; we can rename the table column, add the new column to the table, delete the column from the table, and add constraints to the table. We can alter the column in PostgreSQL by using an alter table statement; using an alter table statement, we have to add a new column, renaming an existing column, and changing the column’s data type in PostgreSQL. We can modify the table of columns per the user’s requirement.
How to Alter Column in PostgreSQL?
Below are the methods for altering a column in PostgreSQL. To alter a column in PostgreSQL, a user has superuser privileges or table owner privileges on the same table to alter the column in PostgreSQL.
Below is the example that we need to have table owner privileges on the table to the same.
- In the first example, we have trying to add a column using the db_test user, but it will issue the error of “ERROR: must be the owner of relation stud1” because the db_test user has not have sufficient privileges to alter the column.
- In the second example, we have used a Postgres user; Postgres users have sufficient privileges to add columns in PostgreSQL.
Code #1
psql -U db_test -d testing
alter table stud1 ADD stud_id_new int;
psql -U postgres -d testing
alter table stud1 ADD stud_id_new int;
\d+ stud1;
Output:
Using the stud2 and student table to describe the example of altering a column in PostgreSQL is as follows. Below is the table description of the student and stud2 table.
Code #2
\d+ student;
\d+ stud2;
Output:
Methods of Alter Column in PostgreSQL
Below is the method of the same:
1. Rename Column Name
In the below example, we have used alter table command to change an existing column’s name. Below is the syntax of the alter table to change the name of the column.
Syntax
Alter table existing_table_name RENAME COLUMN old_column_name TO new_column_name;
In the above syntax, the alter table statement is defined as changing the column’s name using the alter table statement. Rename column is defined as rename an existing column with a new name.
Code:
ALTER TABLE student RENAME COLUMN stud_name TO stud_name_new;
\d+ student;
Output:
In the above example, we have to change the table column name from stud_name to stud_name_new.
2. Add New Column
In the example below, we added a new column to the table using the alter table statement. Below is the syntax of the alter table to add a new column are as follows.
Syntax:
Alter table name_of_tableADD COLUMN column_name (Name of column which we have adding) data_type;
In the above syntax, the alter table statement is defined as add the new column using the alter table statement. Add column is defined as add a new column to an existing table.
Code:
ALTER TABLE student ADD COLUMN address varchar;
\d+ student;
Output:
3. Delete Column
We have to delete a column from the table by using the alter table statement in PostgreSQL. Below is the syntax of the alter table to delete the column.
Syntax
Alter table name_of_tableDROP COLUMN column_name (Name of column which we have deleting);
The alter table statement is defined as drop a new column using the alter table statement in the above syntax. Drop column is the keyword used to deleting the column.
Code:
ALTER TABLE student DROP COLUMN address;
\d+ student;
Output:
4. Add Constraints
In the below example, we have adding constraints. Below is the syntax for adding constraint in PostgreSQL.
Syntax
Alter table name_of_tableALTER COLUMN name_of_columnset name_of_constraint;
The above syntax defines the alter table statement as adding a constraint to a column. Alter column is used to add a constraint to the column.
Code:
Alter table stud2 ALTER COLUMN id set NOT NULL;
\d+ stud2;
Output:
5. Remove Constraint
In the below example, we have to remove the constraint. Below is the syntax of removing constraint.
Syntax:
Alter table name_of_tableDROP CONTRAINT name_of_constraint;
The above syntax defines the alter table statement as a dropping constraint from the column. Drop constraint is used to drop constraint from the column.
Code:
ALTER TABLE student DROP CONSTRAINT "student_pkey";
\d+ student;
Output:
6. Data Type of Column
In the example below, we have to change the column’s data type. Below is the syntax to change the data type.
Syntax:
Alter table name_of_tableALTER COLUMN name_of_column data_type;
The above syntax defines the alter table statement as changing the column data type. We use the “ALTER COLUMN” command to change the data type of a column.
Code:
Alter table student ALTER COLUMN phone type varchar(10);
\d+ student;
Output:
Recommended Articles
We hope that this EDUCBA information on “Alter Column in PostgreSQL” was beneficial to you. You can view EDUCBA’s recommended articles for more information.