ALTER TABLE (Alter Table)

Last updated: 2026-11-04

ALTER TABLE (Alter Table)

ALTER TABLE changes the shape of an existing table: adding or dropping columns, changing column types, adding or dropping constraints, renaming, and setting table-level options. It is the workhorse of DDL. Since PostgreSQL 11, adding a column with a DEFAULT is a pure metadata operation that does not rewrite the table.

Kindcommand
Example LanguageSQL

📝 Syntax

ALTER TABLE <table>
    ADD [COLUMN] <column> <data type> [DEFAULT <expression>],
    ALTER [COLUMN] <column> [SET DATA] TYPE <new type> [USING <expression>],
    ADD CONSTRAINT <name> <constraint definition> [NOT VALID],
    DROP CONSTRAINT [IF EXISTS] <name>,
    DROP [COLUMN] [IF EXISTS] <column> [CASCADE | RESTRICT],
    RENAME TO <new name>;

⚙️ Parameters

ADD COLUMNAdds a column
ALTER COLUMN ... TYPEChanges a column's data type
ADD CONSTRAINTAdds a table-level constraint
DROP CONSTRAINTDrops a table-level constraint
DROP COLUMNDrops a column and its data
RENAME TORenames the table

Returns:No return value. Once the statement succeeds the change is live in the system catalogs.

▶ Example

Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.

Output:

DROP TABLE
CREATE TABLE
INSERT 0 2
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
id | name  | age | email
---+-------+-----+------
 1 | Alice |  30 | NULL 
 2 | Bob   |  25 | NULL

📌 Key Points

More case studies coming soon.

📚 Related Reference

❓ FAQ

QDoes changing a column type lock the table?
AYes. ALTER COLUMN ... TYPE takes an ACCESS EXCLUSIVE lock and rewrites the table, blocking reads and writes. On a large production table, add a new column, backfill it, and swap names instead.
QHow do I rename a table or a column?
AUse ALTER TABLE t RENAME TO t2; and ALTER TABLE t RENAME COLUMN a TO b; — both are metadata-only and fast.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏