PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Triggers
  • Views
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
Home / PostgreSQL Tutorial / PostgreSQL RENAME COLUMN: Renaming One or More Columns of a Table

PostgreSQL RENAME COLUMN: Renaming One or More Columns of a Table

PostgreSQL RENAME COLUMNSummary: in this tutorial, you will learn how to use the PostgreSQL RENAME COLUMN clause in the ALTER TABLE statement to rename one or more columns of a table.

Introduction to PostgreSQL RENAME COLUMN clause

To rename a column of a table, you use the ALTER TABLE statement with RENAME COLUMN clause as follows:

1
2
ALTER TABLE table_name
RENAME COLUMN column_name TO new_column_name;

In this statement:

  • First, specify the table, which contains the column you want to rename, after the ALTER TABLE clause.
  • Second, provide the column name after the RENAME COLUMN clause.
  • Third, give the new column name after the TO keyword.

The COLUMN keyword in the statement in optional therefore you can omit it as shown in the following statement:

1
2
ALTER TABLE table_name
RENAME column_name TO new_column_name;

For some reasons, if you try to rename a non-existing column, PostgreSQL will issue an error. Unfortunately that PostgreSQL does not provide the IF EXISTS option for the RENAME CLAUSE.

To rename multiple columns, you add each RENAME clause for each column, each clause is separated by a comma (,):

1
2
3
4
ALTER TABLE table_name
RENAME column_name_1 TO new_column_name_1,
RENAME column_name_2 TO new_column_name_2,
...;

If you rename a column that references by other database objects such as views, foreign key constraints, triggers, stored procedures, etc., PostgreSQL will also change all of its dependent objects.

PostgreSQL RENAME COLUMN examples

We will create two new tables customers and customer_groups for demonstration.

1
2
3
4
5
6
7
8
9
10
11
12
13
CREATE TABLE customer_groups (
    id serial PRIMARY KEY,
    name VARCHAR NOT NULL
);
 
CREATE TABLE customers (
    id serial PRIMARY KEY,
    name VARCHAR NOT NULL,
    phone VARCHAR NOT NULL,
    email VARCHAR,
    group_id INT,
    FOREIGN KEY (group_id) REFERENCES customer_groups (id)
);

In addition, we create a new view named customer_data based on the customers and customer_groups tables.

1
2
3
4
5
6
7
8
CREATE VIEW customer_data
AS SELECT
    c.id,
    c.name,
    g.name customer_group
FROM
    customers c
INNER JOIN customer_groups g ON g.id = c.group_id;

The following statement renames the email column of the customers table to contact_email:

1
2
ALTER TABLE customers
RENAME COLUMN email TO contact_email;

Let’s try to rename a column that has dependent objects such as the name column of the customer_groups table:

1
2
ALTER TABLE customer_groups
RENAME COLUMN name TO group_name;

Now, you can check whether the change of the name column was cascaded to the customer_data view:

1
2
3
4
5
6
7
8
9
10
11
12
13
test=# \d+ customer_data;
                       View "public.customer_data"
     Column     |       Type        | Modifiers | Storage  | Description
----------------+-------------------+-----------+----------+-------------
id             | integer           |           | plain    |
name           | character varying |           | extended |
customer_group | character varying |           | extended |
View definition:
SELECT c.id,
    c.name,
    g.group_name AS customer_group
   FROM customers c
     JOIN customer_groups g ON g.id = c.group_id;

As you can see in the view definition, the name column has been changed to group_name.

The following illustrates how to rename two columns name and phone of the customers table to customer_name and contact_phone respectively in a single statement:

1
2
3
ALTER TABLE customers
RENAME COLUMN name TO customer_name,
RENAME COLUMN phone TO contact_phone.

In this tutorial, you have learned how to use the PostgreSQL RENAME COLUMN clause in the ALTER TABLE statement to change the name of one or more columns.

Previous Tutorial: PostgreSQL Change Column Type: Step-by-Step Examples
Next Tutorial: PostgreSQL DROP TABLE

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PostgreSQL Fundamentals

  • PostgreSQL Select
  • PostgreSQL Order By
  • PostgreSQL Select Distinct
  • PostgreSQL Where
  • PostgreSQL LIMIT
  • PostgreSQL IN
  • PostgreSQL Between
  • PostgreSQL Like
  • PostgreSQL Inner Join
  • PostgreSQL Left Join
  • PostgreSQL Full Outer Join
  • PostgreSQL Cross Join
  • PostgreSQL Natural Join
  • PostgreSQL Group By
  • PostgreSQL Having
  • PostgreSQL Union
  • PostgreSQL Intersect
  • PostgreSQL Except
  • PostgreSQL Subquery
  • PostgreSQL Insert
  • PostgreSQL Update
  • PostgreSQL Delete
  • PostgreSQL Data Types
  • PostgreSQL Create Table
  • PostgreSQL Alter Table
  • PostgreSQL Drop Table
  • PostgreSQL Truncate Table
  • PostgreSQL CHECK Constraint
  • PostgreSQL Not-Null Constraint
  • PostgreSQL Foreign Key
  • PostgreSQL Primary Key
  • PostgreSQL UNIQUE Constraint

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • How To Change The Password of a PostgreSQL User
  • PostgreSQL AGE Function
  • PostgreSQL DATE_PART Function
  • PostgreSQL List Users
  • PostgreSQL NOW Function
  • PostgreSQL DATE_TRUNC Function
  • PostgreSQL TO_DATE Function: Convert String to Date
  • A Look at PostgreSQL User-defined Data Types
  • PostgreSQL Copy Database Made Easy
  • How to Get Table, Database, Indexes, Tablespace, and Value Size in PostgreSQL

More Tutorials

  • PostgreSQL Cheat Sheet
  • PostgreSQL Administration
  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2017 by PostgreSQL Tutorial Website. All Rights Reserved.