Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • PostgreSQL - Get Started
  • Install PostgreSQL
  • Connect to PostgreSQL DB
  • Create Database
  • Create Table
  • Copy Table
  • Drop Table
  • Drop Database
  • Truncate Table
  • ALTER Table
  • Rename Table
  • Rename Columns
  • Add Columns
  • Modify Column Type
  • Set Default Value of Column
  • Remove Columns
  • Add Constraints to Table
  • Insert Data
  • Upsert Data
  • Update Data
  • Delete Data
  • SELECT Statement
  • WHERE Clause
  • GROUP BY Clause
  • HAVING Clause
  • ORDER BY Clause
  • DISTINCT Clause
  • Inner Join
  • Left Outer Join
  • Right Outer Join
  • Full Outer Join
  • Self Join
  • Natural Join
  • Cross Join
  • LIMIT OFFSET Clause
  • GROUPING SETS
  • GROUPING() Function
  • GROUP BY CUBE
  • GROUP BY ROLLUP
  • Sub Query
  • ALL Operator
  • ANY Operator
  • UNION Operator
  • INTERSECT Operator
  • EXCEPT Operator
  • IS NULL Operator
  • BETWEEN Operator
  • LIKE Operator
  • CAST Operator
  • CASE Expressions
  • NULLIF()
  • COALESCE()
  • GREATEST(), LEAST()
  • WITH Queries (CTE)
  • Constraints
  • NOT NULL Constraint
  • Unique Constraint
  • Check Constraint
  • Primary Key
  • Foreign Key
  • Sequence
  • Serial Type
  • Identity Columns
  • Generated Columns
  • Data Types
  • Boolean Type
  • Character Type
  • Integer Type
  • Numeric Type
  • Date Type
  • Time Type
  • TimeStamp Type
  • Interval Type
  • Array Type
  • Json Type
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

Add Columns to a Table in PostgreSQL

Use ALTER TABLE ADD COLUMN statement to add a new column to existing table. Postgres does not support adding multiple columns by one ALTER TABLE statement. Hence, if you want to add multiple columns to a table, you need to execute ALTER TABLE command multiple times.

Syntax:
ALTER TABLE [schema_name.]table_name 
ADD COLUMN <column_name> <data_type> [column_constraint];

The column_constraint is optional. You can add new columns to a table with or without adding constraint.

Consider that you already have the following employee table.

Let's add a new 'salary' column into the employee table.

Example: Add NOT NULL Column
ALTER TABLE employee
ADD COLUMN salary INT NOT NULL;

Now, the employee table will have a new salary column, as shown below.

If a table already has data, then a new column cannot be added with NOT NULL constraint. It will raise error that column contains null values. To solve this, you first need to create a nullable column, then insert data in a column, and then add a NOT NULL column constraint.

The follwing will add a nullable column 'salary'.

Example: Add Nullable Column
ALTER TABLE employee
ADD COLUMN salary INT;

Add Column using pgAdmin

You can also use pgAdmin to add columns into a table. Expand Tables node in the left pane and right click on the table name or columns node and select Create -> Column.. in the content menu.

This will open Create Column popup where you can enter column name, data type, constraints, etc.

Click on Save button to add a column into a table.

Alternatively, you can add one or more columns in pgAdmin by right clicking on the the table name and select 'Properties'. In the popup, go to 'Columns' tab wherein you can add multiple columns by clicking on the + symbol and enter column name, data type, length, NOT NULL at the bottom row.

TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.