EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

Alter Column in PostgreSQL

By Priya PedamkarPriya Pedamkar

Home » Data Science » Data Science Tutorials » PostgreSQL Tutorial » Alter Column in PostgreSQL

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, adding the new column to the table, delete the column from the table also have added 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 as per the requirement of the user.

How to Alter Column in PostgreSQL?

Below are the methods of 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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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 sufficient privileges to alter the column.
  • In the second example, we have used Postgres user; Postgres user has sufficient privileges to add column 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 below example, we have added a new column to the table by 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 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 of adding constraint in PostgreSQL.

Syntax

Alter table name_of_tableALTER COLUMN name_of_columnset name_of_constraint;

In the above syntax, the alter table statement is defined 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;

In the above syntax, the alter table statement is defined as 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 below example, we have to change the data type of column. Below is the syntax to change the data type.

Syntax:

Alter table name_of_tableALTER COLUMN name_of_column data_type;

In the above syntax, the alter table statement is defined as changing the column data type. Alter column is used to change the column data type.

Code:

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

Output:

Data Type of Column

Recommended Articles

This is a guide to Alter Column in PostgreSQL. Here we discuss an introduction to Alter Column in PostgreSQL, syntax, methods with examples to implement. You can also go through our other related articles to learn more –

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

PostgreSQL Course (2 Courses, 1 Project)

2 Online Courses

1 Hands-on Project

7+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PostgreSQL Tutorial
  • Basic
    • What is PostgreSQL
    • PostgreSQL Features
    • How to Install PostgreSQL
    • PostgreSQL Versions
    • PostgreSQL Architecture
    • PostgreSQL GUI
    • Postgres Command-Line
    • PostgreSQL Variables
    • PostgreSQL Data Types
    • PostgreSQL NOT NULL
    • PostgreSQL Integer
    • PostgreSQL Boolean
    • PostgreSQL BIGINT
    • PostgreSQL NULLIF
    • PostgreSQL Administration
    • PostgreSQL Commands
    • PostgreSQL Operators
    • PostgreSQL IN Operator
    • Postgres like query
    • PostgreSQL encode
    • PostgreSQL Cheat Sheet
    • PostgreSQL List Databases
    • PostgreSQL Rename Database
  • Control Statement
    • PostgreSQL IF Statement
    • PostgreSQL if else
    • PostgreSQL CASE Statement
    • PostgreSQL LOOP
    • PostgreSQL For Loop
    • PostgreSQL While Loop
  • Joins
    • Joins in PostgreSQL
    • PostgreSQL Inner Join
    • PostgreSQL Outer Join
    • LEFT OUTER JOIN in PostgreSQL
    • PostgreSQL FULL OUTER JOIN
    • PostgreSQL LEFT JOIN
    • PostgreSQL Full Join
    • PostgreSQL Cross Join
    • PostgreSQL NATURAL JOIN
    • PostgreSQL UPDATE JOIN
  • Queries
    • PostgreSQL Queries
    • PostgreSQL INSERT INTO
    • PostgreSQL WHERE Clause
    • PostgreSQL WITH Clause
    • PostgreSQL ORDER BY
    • PostgreSQL ORDER BY Random
    • PostgreSQL ORDER BY DESC
    • PostgreSQL GROUP BY
    • PostgreSQL group_concat
    • PostgreSQL HAVING
    • PostgreSQL Recursive Query
  • Advanced
    • PostgreSQL Schema
    • Postgres List Schemas
    • PostgreSQL Drop Schema
    • PostgreSQL VARCHAR
    • Array in PostgreSQL
    • PostgreSQL DDL
    • PostgreSQL List Users
    • Postgres Default User
    • Postgres add user
    • PostgreSQL User Password
    • PostgreSQL log_statement
    • PostgreSQL repository
    • PostgreSQL shared_buffer
    • PostgreSQL String Functions
    • PostgreSQL Compare Strings
    • PostgreSQL Text Search
    • PostgreSQL TEXT
    • PostgreSQL String Array
    • PostgreSQL where in array
    • PostgreSQL Constraints
    • PostgreSQL UNIQUE Constraint
    • PostgreSQL CHECK Constraint
    • PostgreSQL INTERSECT
    • PostgreSQL Like
    • Cursors in PostgreSQL
    • PostgreSQL UNION ALL
    • Indexes in PostgreSQL
    • PostgreSQL Index Types
    • PostgreSQL REINDEX
    • PostgreSQL UNIQUE Index
    • PostgreSQL Clustered Index
    • PostgreSQL DROP INDEX
    • PostgreSQL DISTINCT
    • PostgreSQL FETCH
    • PostgreSQL RAISE EXCEPTION
    • PostgreSQL Auto Increment
    • Sequence in PostgreSQL
    • Wildcards in PostgreSQL
    • PostgreSQL Subquery
    • PostgreSQL Alias
    • PostgreSQL LIMIT
    • PostgreSQL Limit Offset
    • PostgreSQL LAG()
    • PostgreSQL Table
    • Postgres Show Tables
    • PostgreSQL Describe Table
    • PostgreSQL Lock Table
    • PostgreSQL ALTER TABLE
    • Postgres Rename Table
    • PostgreSQL List Tables
    • PostgreSQL TRUNCATE TABLE
    • PostgreSQL Table Partitioning
    • Postgres DROP Table
    • PostgreSQL Functions
    • PostgreSQL Math Functions
    • PostgreSQL Window Functions
    • Aggregate Functions in PostgreSQL
    • PostgreSQL Primary Key
    • Foreign Key in PostgreSQL
    • PostgreSQL Procedures
    • PostgreSQL Stored Procedures
    • PostgreSQL Views
    • PostgreSQL Materialized Views
    • Postgres Create View
    • PostgreSQL Triggers
    • PostgreSQL DROP TRIGGER
    • PostgreSQL Date Functions
    • PostgreSQL TO_DATE()
    • PostgreSQL datediff
    • PostgreSQL Timestamp
    • PostgreSQL CURRENT_TIMESTAMP()
    • PostgreSQL Notify
    • PostgreSQL LENGTH()
    • PostgreSQL blob
    • PostgreSQL Median
    • PostgreSQL kill query
    • PostgreSQL Formatter
    • PostgreSQL RANK()
    • PostgreSQL Select
    • PostgreSQL Average
    • PostgreSQL DATE_PART()
    • PostgreSQL EXECUTE
    • PostgreSQL COALESCE
    • PostgreSQL EXTRACT()
    • PostgreSQL Sort
    • PostgreSQL TO_CHAR
    • PostgreSQL Interval
    • PostgreSQL Number Types
    • PostgreSQL ROW_NUMBER
    • Alter Column in PostgreSQL
    • PostgreSQL Identity Column
    • PostgreSQL SPLIT_PART()
    • PostgreSQL CONCAT()
    • PostgreSQL replace
    • PostgreSQL TRIM()
    • PostgreSQL MAX
    • PostgreSQL DELETE
    • PostgreSQL Float
    • PostgreSQL OID
    • PostgreSQL log
    • PostgreSQL REGEXP_MATCHES()
    • PostgreSQL MD5 
    • PostgreSQL NOW()
    • PostgreSQL RANDOM
    • PostgreSQL round
    • PostgreSQL Trunc()
    • PostgreSQL TIME
    • PostgreSQL IS NULL
    • PostgreSQL CURRENT_TIME
    • PostgreSQL MOD()
    • Postgresql Count
    • PostgreSQL Datetime
    • PostgreSQL MIN()
    • PostgreSQL age()
    • PostgreSQL enum
    • PostgreSQL OR
    • PostgreSQL Wal
    • PostgreSQL NOT IN
    • PostgreSQL SET
    • PostgreSQL Current Date
    • PostgreSQL Compare Date
    • PostgreSQL SERIAL
    • PostgreSQL UUID
    • PostgreSQL Merge
    • PostgreSQL Database
    • PostgreSQL Clone Database
    • PostgreSQL Copy Database
    • PostgreSQL Show Databases
    • PostgreSQL Restore Database
    • PostgreSQL DROP DATABASE
    • PostgreSQL ALTER DATABASE
    • Postgres DROP Database
    • Postgres Dump Database
    • PostgreSQL OFFSET
    • PostgreSQL GRANT
    • PostgreSQL COMMIT
    • PostgreSQL ROLLUP
    • PostgreSQL JSON
    • EXPLAIN ANALYZE in PostgreSQL
    • PostgreSQL Temporary Table
    • PostgreSQL Show Tables
    • PostgreSQL cluster
    • PostgreSQL Replication
    • PostgreSQL Logical Replication
    • PostgreSQL flush privileges
    • PostgreSQL Tablespaces
    • CAST in PostgreSQL
    • PostgreSQL CTE
    • hstore in PostgreSQL
    • PostgreSQL Encryption
    • PostgreSQL DECODE()
    • PostgreSQL Vacuum
    • PostgreSQL EXCLUDE
    • Postgres Change Password
    • Postgres Delete Cascade
    • PostgreSQL EXCEPT
    • PostgreSQL Roles
    • PostgreSQL Link
    • PostgreSQL Partition
    • PostgreSQL column does not exist
    • PostgreSQL Log Queries
    • PostgreSQL escape single quote
    • PostgreSQL Query Optimization
    • PostgreSQL Character Varying
    • PostgreSQL Transaction
    • PostgreSQL Extensions
    • PostgreSQL Import CSV
    • PostgreSQL Client
    • PostgreSQL caching
    • PostgreSQL Incremental Backup
    • PostgreSQL JSON vs JSONNB
    • PostgreSQL JDBC Driver
    • PostgreSQL Interview Questions
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

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

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

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

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

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

Let’s Get Started

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

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

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

EDUCBA

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

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

Special Offer - PostgreSQL Course (2 Courses, 1 Project) Learn More