TExcelTAD
Home › PostgreSQL › PostgreSQL CREATE TABLE

PostgreSQL CREATE TABLE

Before you can store data you need a table. CREATE TABLE gives the table a name and lists its columns, each with a data type and optional constraints (rules the data must follow).

The basic form

text
CREATE TABLE table_name (
    column_name  data_type  [constraints],
    column_name  data_type  [constraints],
    ...
);

Let's create a table for book reviews:

pgsql · try it yourself
CREATE TABLE reviews (
    id        integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    book_id   integer NOT NULL,
    reviewer  text    NOT NULL,
    rating    integer NOT NULL,
    comment   text,
    posted_on date    DEFAULT '2025-01-01'
);

SELECT column_name, data_type, column_default
FROM information_schema.columns
WHERE table_name = 'reviews'
ORDER BY ordinal_position;
CREATE TABLE
 column_name | data_type |   column_default
-------------+-----------+--------------------
 id          | integer   |
 book_id     | integer   |
 reviewer    | text      |
 rating      | integer   |
 comment     | text      |
 posted_on   | date      | '2025-01-01'::date
(6 rows)

The command tag CREATE TABLE confirms success. Here is what each part means:

Part Meaning
integer, text, date The data type of the column
GENERATED ALWAYS AS IDENTITY PostgreSQL numbers the rows automatically (1, 2, 3, …)
PRIMARY KEY Uniquely identifies each row; cannot be NULL or repeated
NOT NULL A value is required
DEFAULT '2025-01-01' Value used when an INSERT does not supply one
(nothing) The column is optional and may hold NULL

Common data types

Type Use it for Example
integer / bigint whole numbers 42
numeric(p, s) exact decimals, such as money 19.99
text / varchar(n) strings (varchar(n) limits the length) 'Ada'
boolean yes/no true
date, timestamp, timestamptz dates and times '2025-03-01'

The Data Types lesson goes much deeper.

Linking tables with a foreign key

A foreign key makes a column reference the primary key of another table. Here every review must belong to a book that really exists in books, and the rating must be between 1 and 5:

pgsql · try it yourself
CREATE TABLE book_reviews (
    id      integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    book_id integer NOT NULL REFERENCES books (id),
    rating  integer NOT NULL CHECK (rating BETWEEN 1 AND 5)
);

INSERT INTO book_reviews (book_id, rating) VALUES (1, 5);
INSERT INTO book_reviews (book_id, rating) VALUES (999, 4);
INSERT INTO book_reviews (book_id, rating) VALUES (2, 9);
CREATE TABLE
INSERT 0 1
ERROR:  insert or update on table "book_reviews" violates foreign key constraint "book_reviews_book_id_fkey"
DETAIL:  Key (book_id)=(999) is not present in table "books".
ERROR:  new row for relation "book_reviews" violates check constraint "book_reviews_rating_check"
DETAIL:  Failing row contains (3, 2, 9).

The first insert works. The second fails because there is no book 999, and the third breaks the CHECK rule. Constraints protect your data even when application code has bugs.

Errors you will meet

Creating a table that already exists is an error. IF NOT EXISTS turns it into a notice instead, which is useful in setup scripts that may run twice:

pgsql · try it yourself
CREATE TABLE books (id integer);
CREATE TABLE IF NOT EXISTS books (id integer);
ERROR:  relation "books" already exists
NOTICE:  relation "books" already exists, skipping
CREATE TABLE

Note: IF NOT EXISTS only checks the name. If a table with that name exists but has different columns, PostgreSQL still skips the statement silently (with a notice).

Creating a table from a query

CREATE TABLE … AS builds a new table from the result of a SELECT. The new table copies the column names and types, but not the constraints:

pgsql · try it yourself
CREATE TABLE cheap_books AS
SELECT id, title, price FROM books WHERE price < 13;

SELECT * FROM cheap_books ORDER BY price;
SELECT 5
 id |       title        | price
----+--------------------+-------
 14 | Moon Socks         |  7.50
 13 | Ten Tiny Dragons   |  8.99
  6 | Murder at Low Tide | 11.99
  2 | Harbor Lights      | 12.50
 18 | The Map Thief      | 12.99
(5 rows)

The tag SELECT 5 tells you how many rows were copied into the new table.

Test yourself

Which constraint makes a column required?

  1. UNIQUE
  2. DEFAULT
  3. NOT NULL
  4. CHECK

What does REFERENCES books (id) create?

  1. An index on books
  2. A foreign key: values must exist in books.id
  3. A copy of the books table
  4. A view

What does CREATE TABLE IF NOT EXISTS t (...) do when t already exists?

  1. Replaces the table
  2. Raises an error
  3. Does nothing and prints a notice
  4. Adds the new columns