← Back to PostgreSQL, page 2

postgresql-0031 · Easy fresher

Copy a table's structure with CREATE TABLE ... LIKE, and explain what CREATE TABLE AS leaves out.

CREATE TABLE new (LIKE old INCLUDING ALL) copies columns, defaults, constraints, indexes, identity and comments, but no rows. CREATE TABLE new AS SELECT ... copies data and column types only, without primary keys, defaults, constraints or indexes. Choose LIKE for an empty twin such as a staging table, and CREATE TABLE AS for a quick snapshot.

Full answer

Setup for the example:

sql
CREATE TABLE customers (
  id    int GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  email text NOT NULL UNIQUE,
  tier  text DEFAULT 'free' CHECK (tier IN ('free', 'pro'))
);
INSERT INTO customers (email) VALUES ('a@x.in'), ('b@x.in');
sql
CREATE TABLE customers_stage (LIKE customers INCLUDING ALL); CREATE TABLE customers_snap AS SELECT * FROM customers;

Result on PostgreSQL 17:

text
SELECT 2
sql
SELECT c.relname AS table_name, (SELECT count(*) FROM pg_constraint k WHERE k.conrelid = c.oid) AS constraints, (SELECT count(*) FROM pg_index i WHERE i.indrelid = c.oid) AS indexes FROM pg_class c WHERE c.relname IN ('customers', 'customers_stage', 'customers_snap') ORDER BY c.relname;

Result:

text
    table_name   | constraints | indexes
-----------------+-------------+---------
 customers       | 3           | 2
 customers_snap  | 0           | 0
 customers_stage | 3           | 2
(3 rows)
sql
INSERT INTO customers_stage (email) VALUES ('c@x.in') RETURNING id, tier;

Output:

text
 id | tier
----+------
 1  | free
(1 row)
sql
SELECT count(*) AS snap_rows FROM customers_snap;

PostgreSQL returns:

text
 snap_rows
-----------
 2
(1 row)

The LIKE copy has the same constraints and indexes as the original: the primary key, the unique email and the CHECK. NOT NULL is copied as well, although PostgreSQL 17 does not list it in pg_constraint, so it is not in the count. The snapshot has no constraints or indexes at all, though it holds both rows. Inserting into the staging table applies the default tier and generates an id from its own new identity sequence, starting at 1. A snapshot made with CREATE TABLE AS needs keys and indexes added by hand if it will be queried heavily.

sql
CREATE TABLE customers (
  id    int GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  email text NOT NULL UNIQUE,
  tier  text DEFAULT 'free' CHECK (tier IN ('free', 'pro'))
);
INSERT INTO customers (email) VALUES ('a@x.in'), ('b@x.in');

CREATE TABLE customers_stage (LIKE customers INCLUDING ALL); CREATE TABLE customers_snap AS SELECT * FROM customers;

SELECT c.relname AS table_name, (SELECT count(*) FROM pg_constraint k WHERE k.conrelid = c.oid) AS constraints, (SELECT count(*) FROM pg_index i WHERE i.indrelid = c.oid) AS indexes FROM pg_class c WHERE c.relname IN ('customers', 'customers_stage', 'customers_snap') ORDER BY c.relname;

INSERT INTO customers_stage (email) VALUES ('c@x.in') RETURNING id, tier;

SELECT count(*) AS snap_rows FROM customers_snap;

Run on PostgreSQL 17.12 (Debian 17.12-0+deb13u1) in a fresh schema, twice, with identical output.

Key points

  • LIKE INCLUDING ALL copies structure
  • CREATE TABLE AS copies data and types only
  • LIKE creates its own identity sequence
  • Snapshots need indexes added
  • Pick by purpose

Common mistakes

  • Using CREATE TABLE AS and expecting constraints
  • Assuming the copy shares the original sequence

Follow-ups

  • What do INCLUDING DEFAULTS and INCLUDING INDEXES control individually?
  • How would you copy a table between databases?

Tips

  • Count constraints and indexes