CREATE TABLE creates a new table in PostgreSQL: you give it a name and a list of columns, each with a data type. Here’s how to create a table, and how to avoid an error when it may already exist.
Every command and output on this page is real, from PostgreSQL 19 on macOS.
Syntax
CREATE TABLE [ IF NOT EXISTS ] table_name (
column_name data_type [, ...]
);
table_name: the new table’s name. Lowercase letters and underscores are the safe choice.column_name data_type: one column, such asname text. Separate columns with commas.IF NOT EXISTS: if a table with that name is already there, skip it with a notice instead of an error.[ ]marks optional parts.
See CREATE TABLE (opens in a new tab) in the PostgreSQL documentation.
The Example
A market keeps a list of fruits: each fruit’s name and colour.
Step 1: Create a Test Database
Open Terminal and connect to your PostgreSQL server with psql as the postgres user:
psql -U postgres
Type the user’s password when psql asks for it. psql connects you to the postgres database, and its prompt is postgres=#.
Create a database for the example and connect to it:
CREATE DATABASE market;
CREATE DATABASE
\c market
You are now connected to database "market" as user "postgres".
Step 2: Create the Table
CREATE TABLE fruits (name text, color text);
CREATE TABLE
psql answers CREATE TABLE: the table is ready, with no rows yet.
Step 3: Look at the Table
psql’s \d command describes a table:
\d fruits
Table "public.fruits"
Column | Type | Collation | Nullable | Default
--------+------+-----------+----------+---------
name | text | | |
color | text | | |
public is the schema, the part of the database where new tables go by default.
Step 4: Create It Only If It’s Missing
Running the same statement again fails, because the table exists:
CREATE TABLE fruits (name text, color text);
ERROR: relation "fruits" already exists
IF NOT EXISTS skips it with a notice:
CREATE TABLE IF NOT EXISTS fruits (name text, color text);
NOTICE: relation "fruits" already exists, skipping
CREATE TABLE
Clean Up
To remove the example, connect to another database and drop the test database:
\c postgres
You are now connected to database "postgres" as user "postgres".
DROP DATABASE market;
DROP DATABASE
Warning: DROP DATABASE deletes the database and everything in it, and it can’t be undone.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Expecting IF NOT EXISTS to fix a table that exists | It skips the statement and changes nothing, even if the old table has other columns. | Check the existing table with \d. |
FAQ
Which database does the table go into?
The one you’re connected to, the one psql’s prompt names.
Conclusion
CREATE TABLE, a name and a list of columns with their types create a table. Add IF NOT EXISTS when the table may already be there.
Additional Resources
Postgres, PostgreSQL and the Slonik Logo are trademarks or registered trademarks of the PostgreSQL Community Association of Canada, and used with their permission.
0 comments