EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PostgreSQL Tutorial Alter Column in PostgreSQL
 

Alter Column in PostgreSQL

Priya Pedamkar
Article byPriya Pedamkar

Alter Column in PostgreSQL

Introduction to Alter Column in PostgreSQL

Alter column in PostgreSQL used to change the table column structure; we can rename the table column, add the new column to the table, delete the column from the table, and add constraints to the table. We can alter the column in PostgreSQL by using an alter table statement; using an alter table statement, we have to add a new column, renaming an existing column, and changing the column’s data type in PostgreSQL. We can modify the table of columns per the user’s requirement.

 

 

How to Alter Column in PostgreSQL?

Below are the methods for altering a column in PostgreSQL. To alter a column in PostgreSQL, a user has superuser privileges or table owner privileges on the same table to alter the column in PostgreSQL.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Below is the example that we need to have table owner privileges on the table to the same.

  • In the first example, we have trying to add a column using the db_test user, but it will issue the error of “ERROR: must be the owner of relation stud1” because the db_test user has not have sufficient privileges to alter the column.
  • In the second example, we have used a Postgres user; Postgres users have sufficient privileges to add columns in PostgreSQL.

Code #1

psql -U db_test -d testing
alter table stud1 ADD stud_id_new int;
psql -U postgres -d testing
alter table stud1 ADD stud_id_new int;
\d+ stud1;

Output:

Alter Column in PostgreSQL - 1

Using the stud2 and student table to describe the example of altering a column in PostgreSQL is as follows. Below is the table description of the student and stud2 table.

Code #2

\d+ student;
\d+ stud2;

Output:

Alter Column in PostgreSQL - 2

Methods of Alter Column in PostgreSQL

Below is the method of the same:

1. Rename Column Name

In the below example, we have used alter table command to change an existing column’s name. Below is the syntax of the alter table to change the name of the column.

Syntax

Alter table existing_table_name RENAME COLUMN old_column_name TO new_column_name;

In the above syntax, the alter table statement is defined as changing the column’s name using the alter table statement. Rename column is defined as rename an existing column with a new name.

Code:

ALTER TABLE student RENAME COLUMN stud_name TO stud_name_new;
\d+ student;

Output:

Alter Column in PostgreSQL - 3

In the above example, we have to change the table column name from stud_name to stud_name_new.

2. Add New Column

In the example below, we added a new column to the table using the alter table statement. Below is the syntax of the alter table to add a new column are as follows.

Syntax:

Alter table name_of_tableADD COLUMN column_name (Name of column which we have adding) data_type;

In the above syntax, the alter table statement is defined as add the new column using the alter table statement. Add column is defined as add a new column to an existing table.

Code:

ALTER TABLE student ADD COLUMN address varchar;
\d+ student;

Output:

Alter Column in PostgreSQL - 4

3. Delete Column

We have to delete a column from the table by using the alter table statement in PostgreSQL. Below is the syntax of the alter table to delete the column.

Syntax

Alter table name_of_tableDROP COLUMN column_name (Name of column which we have deleting);

The alter table statement is defined as drop a new column using the alter table statement in the above syntax. Drop column is the keyword used to deleting the column.

Code:

ALTER TABLE student DROP COLUMN address;
\d+ student;

Output:

Delete Column

4. Add Constraints

In the below example, we have adding constraints. Below is the syntax for adding constraint in PostgreSQL.

Syntax

Alter table name_of_tableALTER COLUMN name_of_columnset name_of_constraint;

The above syntax defines the alter table statement as adding a constraint to a column. Alter column is used to add a constraint to the column.

Code:

Alter table stud2 ALTER COLUMN id set NOT NULL;
\d+ stud2;

Output:

Add Constraints

5. Remove Constraint

In the below example, we have to remove the constraint. Below is the syntax of removing constraint.

Syntax:

Alter table name_of_tableDROP CONTRAINT name_of_constraint;

The above syntax defines the alter table statement as a dropping constraint from the column. Drop constraint is used to drop constraint from the column.

Code:

ALTER TABLE student DROP CONSTRAINT "student_pkey";
\d+ student;

Output:

Remove Constraint

6. Data Type of Column

In the example below, we have to change the column’s data type. Below is the syntax to change the data type.

Syntax:

Alter table name_of_tableALTER COLUMN name_of_column data_type;

The above syntax defines the alter table statement as changing the column data type. We use the “ALTER COLUMN” command to change the data type of a column.

Code:

Alter table student ALTER COLUMN phone type varchar(10);
\d+ student;

Output:

Data Type of Column

Recommended Articles

We hope that this EDUCBA information on “Alter Column in PostgreSQL” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. PostgreSQL TRIM()
  2. Log PostgreSQL
  3. PostgreSQL REGEXP_MATCHES()
  4. PostgreSQL MD5 

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW