How to ADD COLUMN in PostgreSQL

Galaxy Glossary

How do I add a column to an existing table in PostgreSQL?

ALTER TABLE … ADD COLUMN creates a new column in an existing PostgreSQL table without rewriting unaffected data.

Sign up for the latest in SQL knowledge from the Galaxy Team!

Description

Why use ALTER TABLE ADD COLUMN?

Add new attributes to a live table without downtime, enabling feature releases and analytics enhancements.

What is the exact syntax?

Use ALTER TABLE table_name ADD [COLUMN] column_name data_type [constraint] [options]; to append a column. Omitting COLUMN is allowed.

Which data types can be added?

Any valid PostgreSQL type (text, numeric, jsonb, etc.). Choose defaults carefully to avoid table rewrites.

How to add a nullable column quickly?

ALTER TABLE Customers ADD COLUMN phone text; executes instantly because NULL needs no storage.

How to set a default value safely?

Phase it: add column nullable, backfill, then attach the DEFAULT to prevent full-table rewrite on large tables.

Example backfill pattern

ALTER TABLE Orders ADD COLUMN status text; UPDATE Orders SET status = 'pending'; ALTER TABLE Orders ALTER COLUMN status SET DEFAULT 'pending' NOT NULL;

Can I add multiple columns at once?

Yes. ALTER TABLE Products ADD COLUMN weight numeric, ADD COLUMN color text;

Practical ecommerce example

Add a delivered_at timestamp to Orders to track fulfillment.

ALTER TABLE Orders
ADD COLUMN delivered_at timestamptz;

Verify the addition

\d+ Orders in psql or query information_schema.columns.

Best practices

1. Always qualify table names in multi-schema setups.
2. Use transactions to bundle schema and data changes.
3. Communicate changes via migration files and code reviews.

Common mistakes

Forgetting to backfill before NOT NULL – leads to immediate error. Backfill first.
Applying DEFAULT on huge tables – causes rewrite and locking. Use phased approach.

What if I need to roll back?

Run ALTER TABLE … DROP COLUMN inside the same transaction if still pending. Otherwise create a new migration to remove it.

Where is performance impacted?

Only when adding a NOT NULL column with a non-null default; PostgreSQL rewrites every row. Plan maintenance windows for large tables.

Security considerations

Grant ALTER privilege cautiously. Use role-based access to limit who can change schemas.

Why How to ADD COLUMN in PostgreSQL is important

How to ADD COLUMN in PostgreSQL Example Usage


-- Add a NULLABLE JSONB column to store dynamic attributes
ALTER TABLE Products
    ADD COLUMN attributes jsonb;

-- Phase in a NOT NULL column without rewrite
ALTER TABLE OrderItems ADD COLUMN unit_price numeric;
UPDATE OrderItems oi
SET unit_price = p.price
FROM Products p
WHERE p.id = oi.product_id;
ALTER TABLE OrderItems ALTER COLUMN unit_price SET NOT NULL;

How to ADD COLUMN in PostgreSQL Syntax


ALTER TABLE table_name
    ADD [COLUMN] column_name data_type [DEFAULT expression]
    [COLLATE collation]
    [NOT NULL | NULL]
    [CHECK (expression)]
    [AFTER existing_column] -- MySQL only, ignored in PostgreSQL
    [CASCADE | RESTRICT];

-- Ecommerce example
ALTER TABLE Customers
    ADD COLUMN loyalty_points integer DEFAULT 0 NOT NULL;

Common Mistakes

Frequently Asked Questions (FAQs)

Does ADD COLUMN lock the table?

Only if a full rewrite is needed (NOT NULL with default). Simple nullable adds are almost instant.

Can I add a column with a foreign key in one statement?

Yes. ALTER TABLE Orders ADD COLUMN customer_id integer REFERENCES Customers(id);

How to add a column to all partitions?

Run ALTER TABLE on the parent partitioned table; PostgreSQL propagates the new column to partitions.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo