A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/postgresql/postgresql-add-column/ below:

PostgreSQL - ADD COLUMN - GeeksforGeeks

PostgreSQL - ADD COLUMN

Last Updated : 15 Jul, 2025

In PostgreSQL, the ADD COLUMN statement is a powerful command used to modify an existing database table by adding one or more new columns. This feature is important for adapting table structures to meet evolving data requirements, and it plays a key role in database management and optimization.

In this article, we will explain how to effectively use the ADD COLUMN statement in PostgreSQL, with practical examples, detailed explanations, and tips for optimizing its use. We’ll also discuss common scenarios and best practices to help us get the most out of this command.

What is PostgreSQL ADD COLUMN?

The ADD COLUMN statement is part of the ALTER TABLE command in PostgreSQL, allowing us to add new columns to an existing table. Once added, the new column will be appended to the end of the table, and we can specify attributes such as the column's data type, default value, and constraints. However, it’s important to note that PostgreSQL doesn’t support directly setting the position of the new column within the table.

Syntax

ALTER TABLE table_name
ADD COLUMN new_column_name data_type;

Parameters

Key Points Examples of PostgreSQL ADD COLUMN

Let us take a look at ADD COLUMN Statement in PostgreSQL to better understand the concept and explore how it can be applied to real-world database scenarios effectively

Example 1: Adding a Column to the Village Table

First, create a table named 'village' which contains two columns 'village_id' and 'village_name' columns:

Step 1: Create the table
CREATE TABLE village(
village_id SERIAL PRIMARY KEY,
village_name VARCHAR NOT NULL
);
Step 2: Add a new column to the table

To add a district column of type VARCHAR, we use the following ALTER TABLE statement:

ALTER TABLE village
ADD COLUMN district VARCHAR;
Step 3: Verify the changes

Now we can verify if the column has been added using the below statement:

SELECT * FROM village;

Output

Explanation:

Example 2: Adding a Column to the Cars Table

First, create a table named 'cars' with 'car_id' and 'car_name' columns:

Step 1: Create the table
CREATE TABLE cars(
car_id SERIAL PRIMARY KEY,
car_name VARCHAR NOT NULL
);
Step 2: Add a new column with a default value

Now we add a model column with a default value of 'Unknown' to the table as below

ALTER TABLE cars
ADD COLUMN model VARCHAR;
Step 3: Verify the changes

Now we can verify if the column has been added using the below statement:

SELECT * FROM cars;

Output

Explanation:

Important Points About ALTER TABLE ADD COLUMN Statement Conclusion

The PostgreSQL ADD COLUMN statement is a powerful and flexible tool for modifying the structure of our database tables. By using this command, we can easily add new columns to adapt evolving data needs. While PostgreSQL appends new columns to the end of the table, we can manage column attributes like default values, unique constraints, and data types to meet our specific requirements.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4