WikiGalaxy

Personalize

PostgreSQL Rename Column

Introduction:

Renaming a column in PostgreSQL is a common task that allows you to update the name of a column without altering the data within it. This operation is useful when you want to make column names more descriptive or when standardizing naming conventions across your database schema.

Basic Syntax

Syntax Explanation:

The basic syntax for renaming a column in PostgreSQL is as follows:


        ALTER TABLE table_name
        RENAME COLUMN old_column_name TO new_column_name;
      

Here, table_name is the name of the table containing the column, old_column_name is the current name of the column, and new_column_name is the new name you want to assign to the column.

Example 1: Renaming a Simple Column

Scenario:

Consider a table named employees with a column name. You want to rename this column to full_name for clarity.


        ALTER TABLE employees
        RENAME COLUMN name TO full_name;
      

This command changes the column name from name to full_name in the employees table.

Example 2: Renaming a Column with Constraints

Scenario:

If a column has constraints, such as NOT NULL, the renaming operation will still succeed without affecting the constraint.


        ALTER TABLE orders
        RENAME COLUMN order_date TO purchase_date;
      

The column order_date is renamed to purchase_date while keeping its constraints intact.

Example 3: Renaming Multiple Columns

Scenario:

To rename multiple columns, execute separate ALTER TABLE statements for each column.


        ALTER TABLE products
        RENAME COLUMN price TO cost;

        ALTER TABLE products
        RENAME COLUMN description TO details;
      

First, the column price is renamed to cost, and then description is renamed to details in the products table.

Example 4: Renaming a Column with Indexes

Scenario:

Renaming a column that has indexes will automatically update the index definitions to use the new column name.


        ALTER TABLE inventory
        RENAME COLUMN item_id TO product_id;
      

The column item_id is renamed to product_id, and any indexes referencing item_id are updated accordingly.

Example 5: Renaming a Column in a Large Table

Scenario:

Renaming a column in a large table does not require rewriting the table, making it a fast operation.


        ALTER TABLE transactions
        RENAME COLUMN txn_id TO transaction_id;
      

Even for large tables, the column txn_id is swiftly renamed to transaction_id without performance issues.

Example 6: Handling Dependent Views

Scenario:

Renaming a column will affect views that depend on it. Ensure views are updated to reflect the new column name.


        ALTER TABLE customers
        RENAME COLUMN cust_id TO customer_id;
      

After renaming cust_id to customer_id, update any views using this column.

Example 7: Renaming a Column with Foreign Keys

Scenario:

Renaming a column involved in a foreign key relationship automatically updates the foreign key constraint.


        ALTER TABLE orders
        RENAME COLUMN customer_id TO client_id;
      

The foreign key constraint is updated from customer_id to client_id seamlessly.

Example 8: Renaming a Column with Triggers

Scenario:

Triggers that reference the renamed column need to be reviewed and updated to ensure they function correctly.


        ALTER TABLE audit_log
        RENAME COLUMN log_time TO event_time;
      

After renaming log_time to event_time, verify that triggers referencing this column are updated.

Example 9: Renaming a Column with Inheritance

Scenario:

If a table inherits from another, renaming a column in the parent affects all child tables.


        ALTER TABLE parent_table
        RENAME COLUMN inherited_column TO new_column;
      

All child tables of parent_table will reflect the column name change from inherited_column to new_column.

Example 10: Renaming a Column in a Partitioned Table

Scenario:

Renaming a column in a partitioned table will propagate the change to all partitions.


        ALTER TABLE sales
        RENAME COLUMN region TO sales_region;
      

All partitions of the sales table will have the column region renamed to sales_region.

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025