CREATE TABLE defines a new table with columns, data types, constraints, defaults, and optional storage parameters.
CREATE TABLE builds a brand-new, empty table in the current schema. You declare each column, its data type, optional default value, and any constraints such as PRIMARY KEY or UNIQUE.
Supply the table name and a comma-separated list of column definitions.PostgreSQL will allocate storage and record metadata instantly.
CREATE TABLE Customers (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
email TEXT UNIQUE,
created_at TIMESTAMPTZ DEFAULT NOW()
);
Append PRIMARY KEY, UNIQUE, CHECK, REFERENCES, or DEFAULT clauses after each column or as table-level constraints for multiple columns.
CREATE TABLE OrderItems (
order_id INT,
product_id INT,
quantity INT CHECK (quantity > 0),
PRIMARY KEY (order_id, product_id)
);
TEMPORARY tables vanish at session end—ideal for staging.IF NOT EXISTS prevents errors when the table already exists, making scripts idempotent.
Use WITH (autovacuum_enabled=false) or TABLESPACE to fine-tune performance and disk layout. Only advanced users normally need these options.
• Always set a primary key.
• Use appropriate data types (e.g., NUMERIC for money).
• Document defaults to avoid confusion.
• Keep table and column names lowercase with underscores.
See below for quick fixes to frequent errors.
.
Yes. Use CREATE TABLE new_table AS SELECT ...; to copy structure and data in one step.
Use ALTER TABLE table_name ADD COLUMN column_name data_type; The column will appear with NULL values for existing rows.