CREATE TABLE (Create Table)

Last updated: 2026-11-04

CREATE TABLE (Create Table)

CREATE TABLE creates a new table and defines its columns, types, and constraints. PostgreSQL lets you declare constraints inline: PRIMARY KEY, NOT NULL, UNIQUE, DEFAULT, CHECK, and REFERENCES (foreign keys) may be written at either column or table level, and the database enforces them from that moment on.

Kindcommand
Example LanguageSQL

📝 Syntax

CREATE TABLE <table> (
    <column> <data type> [NOT NULL] [DEFAULT <expression>] [UNIQUE] [CHECK (<condition>)]
        [REFERENCES <parent table>(<parent column>) [ON DELETE <action>]],
    ...,
    PRIMARY KEY (<column>, ...),
    UNIQUE (<column>, ...),
    CHECK (<condition>),
    FOREIGN KEY (<column>) REFERENCES <parent table>(<parent column>)
)

⚙️ Parameters

PRIMARY KEYPrimary key: unique and non-null, with an automatic index
NOT NULLForbids NULL in the column
DEFAULTExpression used when no value is supplied
CHECKCondition every row must satisfy
REFERENCESForeign key pointing at a parent table column
GENERATED ... AS IDENTITYAuto-generated identity column

Returns:No return value. On success the table and its constraints and indexes exist 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
DROP TABLE
CREATE TABLE
CREATE TABLE
INSERT 0 2
INSERT 0 2
id | customer_id | amount | status
---+-------------+--------+-------
 1 |           1 | 120.00 | new   
 2 |           2 |  80.50 | new

📌 Key Points