Skip to main content
0 reactions

How to Rename a Column in PostgreSQL

Featured card for How to Rename a Column in PostgreSQL, with a terminal showing ALTER TABLE readings, RENAME COLUMN temp TO temperature_c, and the Oslo reading kept.
How to Rename a Column in PostgreSQL

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

SyntaxSQL
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:

postgres=#SQL
CREATE DATABASE weather;
OutputPlain text
CREATE DATABASE
postgres=#Plain text
\c weather
OutputPlain text
You are now connected to database "weather" as user "postgres".

Step 2: Create the Table and Add a Reading

weather=#SQL
CREATE TABLE readings (city text, temp numeric);
OutputPlain text
CREATE TABLE
weather=#SQL
INSERT INTO readings (city, temp) VALUES ('Oslo', 4.5);
OutputPlain text
INSERT 0 1

Step 3: Rename the Column

weather=#SQL
ALTER TABLE readings RENAME COLUMN temp TO temperature_c;
OutputPlain text
ALTER TABLE

Here’s the table now:

weather=#SQL
SELECT * FROM readings;
OutputPlain text
 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:

weather=#Plain text
\c postgres
OutputPlain text
You are now connected to database "postgres" as user "postgres".
postgres=#SQL
DROP DATABASE weather;
OutputPlain text
DROP DATABASE

Warning: DROP DATABASE deletes the database and everything in it, and it can’t be undone.

Common Mistakes

MistakeWhat happensDo this instead
Renaming a column that doesn’t exist, for example one you already renamedERROR: column "temp" does not existCheck the current names with \d and the table’s name.
Forgetting the queries that use the old nameThey 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

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