In this article, we will see how to rename a column in PostgreSQL with examples using ALTER TABLE command with RENAME parameter.
To rename a column of a table, you use the ALTER TABLE statement with RENAME COLUMN clause as follows:
Syntax to PostgreSQL Rename column:
ALTER TABLE table_name RENAME COLUMN column_name TO new_column_name;
PostgreSQL Rename column Examples:
Lets create table student:
CREATE TABLE student(SNO int, S_NAME varchar(30), age int);
Get the table structure by running the meta-command.
production=# \d student Table "public.student" Column | Type | Collation | Nullable | Default --------+-----------------------+-----------+----------+--------- sno | integer | | | s_name | character varying(30) | | | age | integer | | |
Now rename column S_NAME to SNAME.
ALTER TABLE student RENAME S_NAME to sname;
Lets verify we have successfully renamed column or not by using meta-command.
production=# \d student Table "public.student" Column | Type | Collation | Nullable | Default --------+-----------------------+-----------+----------+--------- sno | integer | | | sname | character varying(30) | | | age | integer | | |
So, we have explained PostgreSQL rename column with examples.