EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL ALTER TABLE

PostgreSQL ALTER TABLE

By Priya PedamkarPriya Pedamkar

PostgreSQL ALTER TABLE

Introduction to PostgreSQL ALTER TABLE

PostgreSQL alter table statement is used to change the table structure, we can change the table name, and column name, add or drop the table column, add or drop the constraint from the table, set a new schema to the table. To use alter table statement in PostgreSQL, we need to have the owner of table privileges or superuser privileges to execute the alter table statement. Alter table statement is used to delete, add, and modify the structure of the existing table in PostgreSQL.

Syntax of PostgreSQL ALTER TABLE

Below is the syntax :

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

1. Alter table to change the name of the table

Alter table name_of_table RENAME TO new_name_of_table;

2. Alter table to add a column

Alter table name_of_table ADD name_of_column data_type_of_column;

3. Alter table to drop column

Alter table name_of_table DROP name_of_column;

4. Alter table to change the data type of the column

Alter table name_of_tableALTER COLUMN name_of_column type data_type_of_column;

5. Alter table to set a new schema

Alter table name_of_table SET SCHEMA new_schema_name;

6. Alter table to rename the column 

Alter table name_of_table rename column old_column_name to new_column_name;

7. Alter column to add a not-null constraint 

Alter table name_of_table modify name_of_column data_type not null;

8. Alter table to drop constraint 

Alter table name_of_table drop constraint name_of_constraint;

9. Alter table to change the tablespace 

Alter table name_of_table set tablespace name_of_tablespace;

Parameters

Below is the parameter description syntax of the alter table statement in PostgreSQL.

  1. Alter table –This statement is used to change the table structure in PostgreSQL.
  2. Name of the table –This is defined as the table name we used to change the table structure.
  3. Rename to – We use the RENAME TO keyword to change the name of a table. We can use the rename keyword to change the table name.
  4. The new name of the table –This is the newly created name of the table which was we have modified with alter table statement.
  5. Add column –We can add a column of the table by using add column keyword with alter table statement.
  6. Column name – The column name is the name of the column that we have changed using the ALTER TABLE statement.
  7. The data type of the column –This is defined as the data type which was we have allocated to the column.
  8. Modify – We use the MODIFY keyword to modify a column and add a NOT NULL constraint in PostgreSQL.
  9. Set tablespace – We use the SET TABLESPACE command in the ALTER TABLE statement to assign a new tablespace to a table.
  10. Set schema – The SET SCHEMA command, used with the ALTER TABLE statement, sets a new schema for a table.
  11. Drop constraint – The DROP CONSTRAINT command is used in conjunction with the ALTER TABLE statement to remove a constraint from a table.

How to ALTER TABLE statement work in PostgreSQL?

Below is the working of the alter table statement in PostgreSQL. The ALTER TABLE command in PostgreSQL modifies the structure of a table.

The below example shows that we need to have privileges of the owner of the table or superuser privileges.

psql -U db_test -d testing
ALTER TABLE student DROP stud_id;
psql -U Postgres
\c testing
ALTER TABLE student DROP stud_id;
\d+ student;

PostgreSQL ALTER TABLE output 1

In the above first example, we have used the user as db_test, this user doesn’t have the privileges of table owner or superuser, so it will issue an error while executing the alter table statement.

In the second example, we have altered the table using the username as Postgres, after using this user, we have to drop the column name of stud_id from the student table.

Alter table is used to do the following action on the table are as follows.

  1. Change the name of the table.
  2. Add column.
  3. Drop column.
  4. Change the schema of the table.
  5. Change the tablespace of the table.
  6. Add a constraint to the table.
  7. Drop constraint from the table.
  8. Rename column.
  9. Add not null constraint.

Examples

Below is an example of the alter table statement in PostgreSQL are as follows. We have using stud1 and stud2 tables to describe examples of the alter table statement.

Below is the table description of stud1 and stud2 table.

\d+ stud1;
\d+ stud2;

PostgreSQL ALTER TABLE output 2

Example #1 – Alter table to change the name of the table

Below example shows that alter a table to change the name of the table. We have to change the stud1 table name to student1.

Alter table stud1 RENAME TO student1;
\d+ student1;

PostgreSQL ALTER TABLE output 3

Example #2 – Alter table to add a new column

Below example shows that alter the table to add the new column. We have added the test_stud column to the student1 table.

Alter table student1 ADD test_stud varchar;
\d+ student1;

PostgreSQL ALTER TABLE output 4

Example #3 – Alter table to drop column

Below example shows that alter the table to drop the column. We have dropped the test_stud column from the student1 table.

Alter table student1 DROP COLUMN test_stud;
\d+ student1;

PostgreSQL ALTER TABLE output 5

Example #4 – Alter table to change the schema of the table

The below example shows that change in the schema of the stud2 table. We have changed the schema public to my_schema.

Alter table stud2 SET SCHEMA my_schema;
\d+ my_schema.stud2;

output 6

Example #5 – Alter the table to change the tablespace of a table

The below example shows that change in the tablespace of the stud2 table. We have changed tablespace from default to my_test.

Alter table my_schema.stud2 set tablespace my_test;
\d+ my_schema.stud2;

output 7

Example #6 – Alter the table to rename the column

Below example shows that alter the table to rename the column. We have to change the id column to the id1 column.

Alter table my_schema.stud2 rename column id to id1;
\d+ my_schema.stud2;

output 8

Recommended Articles

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

  1. PostgreSQL CTE
  2. PostgreSQL Inner Join
  3. log in PostgreSQL
  4. Array in PostgreSQL
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
Financial Analyst Masters Training Program
1000+ Hours of HD Videos
43 Learning Paths
250+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Data Science Bundle
1500+ Hour of HD Videos
80 Learning Paths
360+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Software Development Bundle
3000+ Hours of HD Videos
149 Learning Paths
600+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
All in One Data Science Bundle1500+ Hour of HD Videos | 80 Learning Paths | 360+ Courses | Verifiable Certificate of Completion | Lifetime Access
Financial Analyst Masters Training Program1000+ Hours of HD Videos | 43 Learning Paths | 250+ Courses | Verifiable Certificate of Completion | Lifetime Access
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - 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

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
Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*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?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more