Site icon JD Bots

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

Syntax

CREATE TABLE [ IF NOT EXISTS ] table_name (
    column_name data_type [, ...]
);

See CREATE TABLE 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:

Terminal

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=#

CREATE DATABASE market;

Output

CREATE DATABASE

postgres=#

\c market

Output

You are now connected to database "market" as user "postgres".

Step 2: Create the Table

market=#

CREATE TABLE fruits (name text, color text);

Output

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=#

\d fruits

Output

             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=#

CREATE TABLE fruits (name text, color text);

Output

ERROR:  relation "fruits" already exists

IF NOT EXISTS skips it with a notice:

market=#

CREATE TABLE IF NOT EXISTS fruits (name text, color text);

Output

NOTICE:  relation "fruits" already exists, skipping
CREATE TABLE

Clean Up

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

market=#

\c postgres

Output

You are now connected to database "postgres" as user "postgres".

postgres=#

DROP DATABASE market;

Output

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.

Exit mobile version