PostgreSQL CREATE TABLE
Summary: in this tutorial, you will learn how to use the PostgreSQL CREATE TABLE statement to create a new table.
Introduction to PostgreSQL CREATE TABLE statement
Typically, a relational database consists of multiple related tables. Tables allow you to store structured data like customers, products, and employees.
To create a new table, you use the CREATE TABLE
statement. Here’s the basic syntax of the CREATE TABLE
statement:
In this syntax:
First, specify the name of the table that you want to create after the CREATE TABLE
keywords. The table name must be unique in a schema. If you create a table with a name that already exists, you’ll get an error.
A schema is a named collection of database objects including tables. If you create a table without a schema, it defaults to public. You’ll learn more about the schema in the schema tutorial.
Second, use the IF NOT EXISTS
option to create a new table only if it does not exist. When you use the IF NOT EXISTS
option and the table already exists, PostgreSQL will issue a notice instead of an error.
Third, specify table columns separated by commas. Each column definition consists of the column name, data type, size, and constraint.
The constraint of a column specifies a rule that is applied to data within a column to ensure data integrity. The column constraints include primary key, foreign key, not null, unique, check, and default.
For example, the NOT NULL
constraint ensures that the values in a column cannot be NULL.
Finally, specify constraints for the table including primary key, foreign key, and check constraints.
A table constraint is a rule that is applied to the data within the table to maintain data integrity.
Note that some column constraints can be defined as table constraints such as primary key, foreign key, unique, and check constraints.
Constraints
PostgreSQL includes the following column constraints:
- NOT NULL– ensures that the values in a column cannot be
NULL
. - UNIQUE – ensures the values in a column are unique across the rows within the same table.
- PRIMARY KEY – a primary key column uniquely identifies rows in a table. A table can have one and only one primary key. The primary key constraint allows you to define the primary key of a table.
- CHECK – ensures the data must satisfy a boolean expression. For example, the value in the price column must be zero or positive.
- FOREIGN KEY – ensures that the values in a column or a group of columns from a table exist in a column or group of columns in another table. Unlike the primary key, a table can have many foreign keys.
Table constraints are similar to column constraints except that you can include more than one column in the table constraint.
PostgreSQL CREATE TABLE example
We will create a new table called accounts
in the dvdrental
sample database. The accounts
table has the following columns:
user_id
– primary keyusername
– unique and not nullpassword
– not nullemail
– unique and not nullcreated_at
– not nulllast_login
– null
The following example uses the CREATE TABLE
statement to create the accounts
table:
To create a table in a database, you need to execute the CREATE TABLE
statement using a PostgreSQL client such as psql and pgAdmin.
We’ll show you step-by-step how to create the accounts
table using the psql client tool.
First, open the Command Prompt on Windows or Terminal on Unix-like systems and connect to the PostgreSQL:
It’ll prompt you to enter a password for the user postgres
.
When you enter a password correctly, you’ll see the following command prompt:
Second, connect to the dvdrental
database:
Third, enter the following CREATE TABLE
statement and press Enter:
Output:
The output indicates that the table has been created.
To view the accounts table, you can use the \d
command:
Output:
Summary
- Use the
CREATE TABLE
statement to create a new table. - Use the
IF NOT EXISTS
option to create the new table only if it does not exist.