In this PostgreSQL post explains how to use the PostgreSQL CREATE TABLE statement with syntax and examples.
The PostgreSQL CREATE TABLE statement allows you to create and define a table.
In its simplest form, the syntax for the CREATE TABLE statement in PostgreSQL is:
CREATE TABLE table_name
(
column1 datatype [ NULL | NOT NULL ],
column2 datatype [ NULL | NOT NULL ],
...
);
However, the full syntax for the PostgreSQL CREATE TABLE statement is:
CREATE [ [ GLOBAL TEMPORARY
| GLOBAL TEMP
| LOCAL TEMPORARY
| LOCAL TEMP
| UNLOGGED ]
TABLE [IF NOT EXISTS] table_name
(
column1 datatype [ COLLATE collation ]
[ CONSTRAINT constraint_name ]
{ NULL
| NOT NULL
| CHECK ( expression ) [ NO INHERIT ]
| DEFAULT default_value
| UNIQUE index_parameters
| PRIMARY KEY index_parameters
| REFERENCES ref_table [ ( ref_column ) ]
[ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ]
[ ON DELETE action ]
[ ON UPDATE action ] }
[ DEFERRABLE | NOT DEFERRABLE ]
[ INITIALLY DEFERRED | INITIALLY IMMEDIATE ],
column2 datatype [ COLLATE collation ]
[ CONSTRAINT constraint_name ]
{ NULL
| NOT NULL
| CHECK ( expression ) [ NO INHERIT ]
| DEFAULT default_value
| UNIQUE index_parameters
| PRIMARY KEY index_parameters
| REFERENCES ref_table [ ( ref_column ) ]
[ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ]
[ ON DELETE action ]
[ ON UPDATE action ] }
[ DEFERRABLE | NOT DEFERRABLE ]
[ INITIALLY DEFERRED | INITIALLY IMMEDIATE ],
...
| [ CONSTRAINT constraint_name ]
{ CHECK ( expression ) [ NO INHERIT ]
| UNIQUE ( index_col_name, ... )
| PRIMARY KEY ( index_col_name, ... )
| FOREIGN KEY ( index_col_name, ... )
REFERENCES another_table_name (index_col_name, ...)
[ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ]
[ ON DELETE action ]
[ ON UPDATE action ]
| LIKE source_table
{ INCLUDING | EXCLUDING }
{ DEFAULTS | CONSTRAINTS | INDEXES | STORAGE | COMMENTS | ALL }
[ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]
[ TABLESPACE tablespace_name ]
);
Optional. If either of these are specified, he table is a global temporary table.
Optional. If either of these are specified, the table is a local temporary table.
Optional. If specified, the data in the table is not written to the write-ahead log. This improves performance on the table, however, the data in this table will be lost if a crash occurs.
Optional. If specified, the CREATE TABLE statement will not raise an error if the tables already exists.
The name of the table that you wish to create.
The columns that you wish to create in the table.
The data type for the column.
Optional. The name of the constraint.
Each column should be defined as NULL or NOT NULL. If this parameter is omitted, the database assumes NULL as the default.
Optional. It is the value to assign to the column if left blank or NULL.
Let's look at a PostgreSQL CREATE TABLE example.
CREATE TABLE order_details
( order_detail_id integer CONSTRAINT order_details_pk PRIMARY KEY,
order_id integer NOT NULL,
order_date date,
quantity integer,
notes varchar(200)
);
This PostgreSQL CREATE TABLE example creates a table called order_details which has 5 columns and one primary key:
You could alternatively have written the CREATE TABLE statement as follows:
CREATE TABLE order_details
( order_detail_id integer NOT NULL,
order_id integer NOT NULL,
order_date date,
quantity integer,
notes varchar(200),
CONSTRAINT order_details_pk PRIMARY KEY (order_detail_id)
);
The difference between the two CREATE TABLE statements is how the PRIMARY KEY is defined. Both methods are acceptable in PostgreSQL.
Next, let's create a table that has a DEFAULT VALUE.
CREATE TABLE order_details
( order_detail_id integer NOT NULL,
order_id integer NOT NULL,
order_date date,
quantity integer,
notes varchar(200) NOT NULL DEFAULT 'Standard shipping',
CONSTRAINT order_details_pk PRIMARY KEY (order_detail_id)
);
This PostgreSQL CREATE TABLE example creates a table called order_details which has 5 columns and one primary key: