Skip to main content
0 reactions

How to Create a Table in PostgreSQL

Featured card for How to Create a Table in PostgreSQL, with a terminal window listing CREATE TABLE fruits, name text, color text, the psql describe command and IF NOT EXISTS.
How to create a table in PostgreSQL.

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

SyntaxSQL
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 as name 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:

TerminalBash
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:

postgres=#SQL
CREATE DATABASE market;
OutputPlain text
CREATE DATABASE
postgres=#Plain text
\c market
OutputPlain text
You are now connected to database "market" as user "postgres".

Step 2: Create the Table

market=#SQL
CREATE TABLE fruits (name text, color text);
OutputPlain 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:

market=#Plain text
\d fruits
OutputPlain text
             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:

market=#SQL
CREATE TABLE fruits (name text, color text);
OutputPlain text
ERROR:  relation "fruits" already exists

IF NOT EXISTS skips it with a notice:

market=#SQL
CREATE TABLE IF NOT EXISTS fruits (name text, color text);
OutputPlain text
NOTICE:  relation "fruits" already exists, skipping
CREATE TABLE

Clean Up

To remove the example, connect to another database and drop the test database:

market=#Plain text
\c postgres
OutputPlain text
You are now connected to database "postgres" as user "postgres".
postgres=#SQL
DROP DATABASE market;
OutputPlain text
DROP DATABASE

Warning: DROP DATABASE deletes the database and everything in it, and it can’t be undone.

Common Mistakes

MistakeWhat happensDo this instead
Expecting IF NOT EXISTS to fix a table that existsIt 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