To rename a column in PostgreSQL, use ALTER TABLE … RENAME COLUMN with the column’s current name and the new one. The column keeps its type and its data; only the name changes. Here’s how, with a small example.
Every command and output on this page is real, from PostgreSQL 19 on macOS.
Syntax
ALTER TABLE table_name
RENAME COLUMN old_name TO new_name;
table_name: the table the column belongs to.old_name: the column’s current name.new_name: the name it gets. No other column in the table may have it already.
See ALTER TABLE (opens in a new tab) in the PostgreSQL documentation.
The Example
A weather station stores its readings with a column named temp, which could mean temperature or temporary. You rename it to temperature_c.
Step 1: Create a Test Database
Connect to your server with psql as the postgres user, as Part 2 of the PostgreSQL Tutorial shows. psql connects you to the postgres database, and its prompt is postgres=#.
Create a database for the example and connect to it:
CREATE DATABASE weather;
CREATE DATABASE
\c weather
You are now connected to database "weather" as user "postgres".
Step 2: Create the Table and Add a Reading
CREATE TABLE readings (city text, temp numeric);
CREATE TABLE
INSERT INTO readings (city, temp) VALUES ('Oslo', 4.5);
INSERT 0 1
Step 3: Rename the Column
ALTER TABLE readings RENAME COLUMN temp TO temperature_c;
ALTER TABLE
Here’s the table now:
SELECT * FROM readings;
city | temperature_c
------+---------------
Oslo | 4.5
(1 row)
The reading is still there, under the new name.
Clean Up
To remove the example, connect to another database and drop the test database:
\c postgres
You are now connected to database "postgres" as user "postgres".
DROP DATABASE weather;
DROP DATABASE
Warning: DROP DATABASE deletes the database and everything in it, and it can’t be undone.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Renaming a column that doesn’t exist, for example one you already renamed | ERROR: column "temp" does not exist | Check the current names with \d and the table’s name. |
| Forgetting the queries that use the old name | They fail, because no column has that name any more. | Update them in the same change. |
FAQ
Does renaming a column change its data?
No. Only the name changes: the type, the values and the column’s place in the table stay the same.
Can I rename two columns in one statement?
No. The documentation (opens in a new tab) says RENAME can’t be combined with other changes in one ALTER TABLE, so rename each column with its own statement.
Conclusion
ALTER TABLE … RENAME COLUMN old_name TO new_name renames a column and keeps its type and data. Remember to update the queries that use the old name.
Additional Resources
- PostgreSQL 19 documentation: ALTER TABLE (opens in a new tab)
- PostgreSQL 19 documentation: Renaming a Column (opens in a new tab)
Related
- PostgreSQL Tutorial — Part 6: Create Tables
- How to Add a Column to a Table in PostgreSQL
- How to Drop a Column in PostgreSQL
Postgres, PostgreSQL and the Slonik Logo are trademarks or registered trademarks of the PostgreSQL Community Association of Canada, and used with their permission.
0 comments