Post­gr­eSQL’s CREATE TABLE command is used to create new tables in a database. When using this command, you can also define various spe­cific­a­tions for the table and in­di­vidu­al columns.

What is Post­gr­eSQL CREATE TABLE?

The CREATE TABLE command in Post­gr­eSQL is used to create a new table in an existing database. When creating a new table, you need to specify a unique name for it as well as assign each column a name and a data type. When creating tables in the popular database man­age­ment system, you can also define con­straints for either all the columns in the table or for in­di­vidu­al columns.

Tip

If you want to modify your table settings later on, you can use the ALTER TABLE command to adjust columns as needed.

What is the syntax for CREATE TABLE?

The basic syntax for Post­gr­eSQL’s CREATE TABLE is as follows:

CREATE TABLE table_name(
column1 data_type PRIMARY KEY,
column2 data_type,
column3 data_type,
…
);
post­gr­esql

The CREATE TABLE command instructs Post­gr­eSQL to create a new table. The command itself is followed by the name for the table, which needs to be unique. A set of brackets is placed directly after the table name, inside of which, you need to define different column names and their cor­res­pond­ing data types.

If you want to add con­straints, the syntax changes:

CREATE TABLE table_name(
column1 data_type PRIMARY KEY constraint,
column2 data_type constraint,
column3 data_type constraint,
…
);
post­gr­esql

In addition to PRIMARY KEY, Post­gr­eSQL also supports the following con­straints:

  • NOT NULL: Ensures a column cannot contain NULL values
  • UNIQUE: Ensures all values in a column or a com­bin­a­tion of columns are unique
  • CHECK: Defines con­di­tions that must be met when inserting or updating data
  • FOREIGN KEY: Es­tab­lishes a re­la­tion­ship with a column in another table
  • DEFAULT: Specifies a default value for a column if no explicit value is provided
Dedicated Server
Per­form­ance through in­nov­a­tion
  • En­ter­prise hardware
  • Con­fig­ur­able hardware equipment
  • ISO-certified data centres

Post­gr­eSQL CREATE TABLE example

To il­lus­trate how CREATE TABLE in Post­gr­eSQL works, we’re going to create a table called customer_list. This table will have four columns: id, name, country and address. The id column is set as the PRIMARY KEY. The NOT NULL con­straint is used to ensure that the id and name columns contain values. Here’s what the code looks like:

CREATE TABLE customer_list(
id INT PRIMARY KEY NOT NULL,
name VARCHAR(50) NOT NULL,
country VARCHAR(50),
address VARCHAR(255)
);
post­gr­esql

This command creates an empty table with the table name and columns that have been specified in the code. You can now fill in the table with data. When populated with data, the table might look similar to this:

id name country address
1 Emily Example United Kingdom 123 High St, Anytown, W1 4GH
2
3

How to check for tables using \d

The \d command lists all the tables within a database and can be used to check if a table was suc­cess­fully created. Here’s how:

testdb-# \d
post­gr­esql

You can also use this command to get a detailed de­scrip­tion of a table. We’ll use the table from above to show you what the code for doing so looks like:

testdb-# \d customer_list
post­gr­esql
Go to Main Menu