w3resource

PostgreSQL Rename Column: Syntax and Practical Example Guide


How to Rename a Column in PostgreSQL

Renaming a column in a PostgreSQL database is a straightforward process that allows developers to adjust table schemas without affecting existing data. This operation can be performed using the ALTER TABLE command with the RENAME COLUMN clause.

This guide provides the syntax, an example, and detailed explanation of how to rename a column in PostgreSQL.


Syntax:

ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name;
  • table_name: Name of the table containing the column.
  • old_column_name: Current name of the column.
  • new_column_name: Desired new name for the column.

Example

Let’s rename a column named username to user_name in a table called users.

Code:


-- Alter the table 'users' to rename the column 'username' to 'user_name'
ALTER TABLE users RENAME COLUMN username TO user_name;

Explanation:

    1. Command Overview: The ALTER TABLE command modifies the structure of an existing table.

    2. RENAME COLUMN: Specifies that the operation involves renaming a column.

    3. Column Names: Provide the current column name followed by the desired new name.

Practical Example

Initial Table Setup

Create a users table with a column named username:

Code:

-- Create the 'users' table with an initial 'username' column
CREATE TABLE users (
    id SERIAL PRIMARY KEY,       -- Primary key for the table
    username VARCHAR(50) NOT NULL, -- Username column
    email VARCHAR(100) NOT NULL   -- Email column
);

Inserting Data

Code:

-- Insert sample data into the 'users' table
INSERT INTO users (username, email) VALUES
('jana_isabel', '[email protected]'),
('david_isabel', '[email protected]');

Renaming the Column

Code:

-- Rename the column 'username' to 'user_name'
ALTER TABLE users RENAME COLUMN username TO user_name;

Verifying the Change

Code:

-- Check the table structure to confirm the column name change
\d users

The result shows the column name username is now user_name.

Explanation

    1. Impact on Data: Renaming a column does not affect the existing data in the table. The change is purely at the schema level.

    2. Dependencies: Ensure no database objects (e.g., triggers, functions, or views) depend on the old column name. If they do, update them accordingly.

    3. Validation: Use \d table_name in psql to confirm the schema changes.

    Best Practices

    • Backup First: Always backup your database before altering the schema.
    • Test Changes: Perform changes in a development or staging environment before applying them in production.
    • Update Code: Rename references to the old column name in your application code.

    All PostgreSQL Questions, Answers, and Code Snippets Collection.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/PostgreSQL/snippets/postgres-rename-column.php