Creating and Managing Tables

1. Creating Table

CREATE TABLE customers (
    id          bigserial PRIMARY KEY,
    email       text UNIQUE NOT NULL,
    full_name   text NOT NULL,
    created_at  timestamptz NOT NULL DEFAULT now()
);
ClausePurpose
column typeOne of PG data types
DEFAULT exprAuto value when not provided
NOT NULLForbid NULL
PRIMARY KEYUnique + NOT NULL
GENERATED ... AS IDENTITYModern auto-increment PREFERRED

2. Creating Table with Constraints

CREATE TABLE orders (
    id          bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    customer_id bigint NOT NULL REFERENCES customers(id) ON DELETE RESTRICT,
    status      text   NOT NULL CHECK (status IN ('new','paid','shipped','cancelled')),
    total_cents integer NOT NULL CHECK (total_cents >= 0),
    placed_at   timestamptz NOT NULL DEFAULT now(),
    CONSTRAINT orders_unique_ref UNIQUE (customer_id, placed_at)
);

3. Creating Temporary Table

CREATE TEMP TABLE staging_orders AS
SELECT * FROM orders WHERE status = 'new';

CREATE TEMP TABLE t (id int) ON COMMIT DROP;
ON COMMITBehavior
PRESERVE ROWSKeep data (default)
DELETE ROWSTruncate at commit
DROPDrop at commit (TX-scoped table)

4. Creating Unlogged Table

CREATE UNLOGGED TABLE cache_entries (
    key text PRIMARY KEY,
    value jsonb,
    expires_at timestamptz
);
PropertyEffect
No WALFaster writes, lost on crash
Not replicatedSkipped by streaming/logical
Use caseCaches, intermediate staging

5. Creating Table from Query

CREATE TABLE big_customers AS
SELECT * FROM customers WHERE lifetime_value > 10000;

CREATE TABLE empty_copy AS SELECT * FROM customers WITH NO DATA;
Note: CREATE TABLE AS copies columns and data but NOT indexes, constraints, defaults.

6. Creating Table Like Another

CREATE TABLE customers_archive (
    LIKE customers INCLUDING ALL EXCLUDING INDEXES
);
OptionWhat It Copies
DEFAULTSDefault expressions
CONSTRAINTSCHECK constraints
INDEXESIndexes (incl. PRIMARY KEY/UNIQUE)
STORAGEPer-column storage
COMMENTSComments
IDENTITYIdentity definition
GENERATEDGenerated column expressions
ALLAll of the above

7. Listing Tables

\dt
\dt+ app.*
SELECT table_schema, table_name
  FROM information_schema.tables
 WHERE table_type='BASE TABLE' AND table_schema NOT IN ('pg_catalog','information_schema');

8. Describing Table Structure

CommandOutput
\d tableColumns, indexes, constraints
\d+ tableAdds storage, stats target
\dS tableInclude system columns
SELECT column_name, data_type, is_nullable, column_default
  FROM information_schema.columns
 WHERE table_schema='app' AND table_name='customers'
 ORDER BY ordinal_position;

9. Renaming Table

ALTER TABLE customers RENAME TO customer;
ALTER TABLE app.customers RENAME COLUMN email_address TO email;

10. Setting Table Owner

ALTER TABLE customers OWNER TO app_owner;
RequirementDetail
CallerMember of new owner role
Existing privilegesPreserved on objects

11. Copying Table Structure and Data

CREATE TABLE backup AS TABLE customers;            -- shorthand
CREATE TABLE clone (LIKE customers INCLUDING ALL);
INSERT INTO clone SELECT * FROM customers;

12. Truncating Table

TRUNCATE TABLE staging;
TRUNCATE TABLE orders, order_items RESTART IDENTITY CASCADE;
OptionEffect
RESTART IDENTITYReset identity/sequences
CONTINUE IDENTITYDefault; keep current
CASCADETruncate FK-referencing tables
RESTRICTDefault; error if FKs reference

13. Dropping Table

DROP TABLE old_orders;
DROP TABLE IF EXISTS old_orders CASCADE;
Warning: CASCADE drops dependent views, FKs, and rules. Audit with pg_depend first.