Basics
- How to Insert
- How to Update
- How to Delete
- How to Trim Strings
- How to Use substring()
- How to Use substring() with RegEx to Extract a String
- How to Replace Substrings
- How to Modify Arrays
- How to Compare Arrays
- How to Concatenate Strings
- How to Convert the Case of a String
- How to Create an Array
- How to Insert Data Into an Array
- How to Query Arrays
- How to Use string_agg()
Database Management
- How to Create a Table
- How to Drop a Table
- How to Rename a Table
- How to Truncate a Table
- How to Duplicate a Table
- How to Add a Column
- How to Change a Column Name
- How to Add a Default Value
- How to Remove a Default Value From a Column
- How to Add a Not Null Constraint
- How to Remove a Not Null Constraint
- How to Add an Index
- How to Drop an Index
- How to Create a View
- How to Drop a View
- How to Reset Sequence
- How to Drop a Column
Dates and Times
Analysis
- How to Use nullif()
- How to Use Lateral Joins
- How to Calculate Percentiles
- How to Get the First Row per Group
- How to Use generate_series to Avoid Gaps in Data
- How to Do Type Casting
- How to Write a Common Table Expression
- How to Import a CSV using Copy
- How to Compare Two Values When One Is Null
- How to Use Coalesce
- How to Write a Case Statement
- How to Use Filter to Have Multiple Counts
- How to Calculate Cumulative Sum-Running Total
- How to Query a JSON Column
How to Change a Column Name in PostgreSQL
Renaming a column in PostgreSQL is a common task when evolving your database schema. Fortunately, PostgreSQL makes this process easy and safe using the ALTER TABLE
command.
Why Rename a Column?
As your application evolves, you might need to rename a column to improve clarity, correct a typo, or reflect new business requirements. Doing this properly ensures your data stays intact and your queries continue to work smoothly.
Basic Syntax
To rename a column, use the following SQL syntax:
ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name;
Example
Let’s say you have a table named customers
with a column called fullname
and you want to rename it to full_name
. You would run:
ALTER TABLE customers RENAME COLUMN fullname TO full_name;
PostgreSQL will automatically update the table definition. Any views, functions, or applications using the old name will need to be updated manually.
Important Considerations
- Check for dependent objects like indexes, views, or triggers that reference the column.
- Update your application code, ORMs, and queries to use the new column name.
- If using replication or logical decoding, test the change in a staging environment first.
Best Practices
- Always make a backup before altering your schema.
- Use a migration tool if you manage your schema with migrations (e.g., Django migrations, Alembic, Liquibase).
- Communicate schema changes with your development team to avoid breaking downstream systems.
Summary
Renaming a column in PostgreSQL is straightforward with ALTER TABLE ... RENAME COLUMN
. Just remember to review dependencies, update your codebase, and follow best practices to ensure a smooth transition.