Preamble
PostgreSQL CREATE TABLE statement allows you to create and define a table
The easiest syntax for CREATE TABLE statement in PostgreSQL
CREATE TABLE table_name
(column1 datatype [ NULL | NOT NULL ],
column2 datatype [ NULL | NOT NULL ],…);
The full syntax for CREATE TABLE in PostgreSQL
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 | INDEXES | STORAGE | COMMENTS | ALL }
[ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]
[ TABLESPACE tablespace_name ]);
Statement parameters and arguments
- GLOBAL TEMPORARY and GLOBAL TEMP – Optional. If one of them is specified, the table is a global time table.
- LOCAL TEMPORARY and LOCAL TEMP – Optional. If one of them is specified, the table is a local time table.
- UNLOGGED – Optional. If specified, the data in the table is not recorded in the pre-recorded log. This improves the performance of the table, but the data in this table will be lost in case of failure.
- IF NOT EXISTS – Optional. If specified, the CREATE TABLE instruction will not cause an error if the tables already exist.
- table_name – The name of the table you want to create.
- column1, column2 – The columns you want to create in the table.
- datatype – The data type for a column.
- CONSTRAINT constraint_name – Optional. Name of the constraint.
- NULL or NOT NULL – Each column must be defined as NULL or NOT NULL. If this parameter is omitted, the database accepts NULL as the default value.
- DEFAULT default_value – Optional. This is the value assigned to the column if it is left blank or NULL.
Consider the PostgreSQL example CREATE TABLE
CREATE TABLE order_details
( order_detail_id integer CONSTRAINT order_details_pk PRIMARY KEY,
order_id integer NOT NULL,
order_date date,
size integer,
notes varchar(200));
This PostgreSQL CREATE TABLE example creates a table with the name order_details, which has 5 columns and one primary key:
- Because it is the table’s primary key, the first column, order detail id, is constructed as an integer data type and cannot contain a NULL value.
- Order id is the second column, an integer data type that cannot include NULL values.
- Order date is the third field, which is a date data type that can contain NULL values.
- The fourth column, amount, is an integer data type that can contain NULL values.
- The fifth column, notes, is a varchar data type with a maximum length of 200 characters and can contain NULL values.
The primary key is called order_details_pk and has a value for the order_detail_id column.
Alternatively, you could write the CREATE TABLE operator as follows
CREATE TABLE order_details
( order_detail_id integer NOT NULL,
order_id integer NOT NULL,
order_date date,
size integer,
notes varchar(200),
CONSTRAINT order_details_pk PRIMARY KEY (order_detail_id));
The difference between the two operators’ CREATE TABLE is how PRIMARY KEY is defined. Both methods are acceptable in PostgreSQL.
PostgreSQL Database and Table creation and Inserting data to the table
About Enteros
Enteros offers a patented database performance management SaaS platform. It proactively identifies root causes of complex business-impacting database scalability and performance issues across a growing number of clouds, RDBMS, NoSQL, and machine learning database platforms.
The views expressed on this blog are those of the author and do not necessarily reflect the opinions of Enteros Inc. This blog may contain links to the content of third-party sites. By providing such links, Enteros Inc. does not adopt, guarantee, approve, or endorse the information, views, or products available on such sites.
Are you interested in writing for Enteros’ Blog? Please send us a pitch!
RELATED POSTS
How Modern Financial Institutions Improve Data Infrastructure with Intelligent Database Analytics
- 6 May 2026
- Database Performance Management
The financial services industry has rapidly evolved into a highly digital and data-driven ecosystem. Modern financial institutions—including banks, fintech companies, and payment providers—depend heavily on data infrastructure to power services such as digital banking, mobile payments, fraud detection, algorithmic trading, and customer analytics. Every interaction within these platforms generates data that must be processed, analyzed, … Continue reading “How Modern Financial Institutions Improve Data Infrastructure with Intelligent Database Analytics”
How to Transform Media Sector Performance with Enteros Cost Estimation, Cost Attribution, and RevOps Efficiency
Introduction The media sector is experiencing a rapid transformation driven by digital platforms, streaming services, real-time content delivery, and data-driven audience engagement. From OTT platforms and digital publishing to live broadcasting and advertising ecosystems, media organizations must operate at scale while delivering high-quality, personalized experiences. However, this evolution comes with significant challenges. Massive volumes of … Continue reading “How to Transform Media Sector Performance with Enteros Cost Estimation, Cost Attribution, and RevOps Efficiency”
Building Resilient Financial Data Platforms with AI-Driven Database Analytics
Financial institutions are experiencing an unprecedented wave of digital transformation. From mobile banking and digital wallets to real-time payments and algorithmic trading, modern financial services rely heavily on high-performing and resilient data platforms. At the heart of these platforms lies the database infrastructure that powers transactions, customer interactions, and data-driven decision-making. However, as financial platforms … Continue reading “Building Resilient Financial Data Platforms with AI-Driven Database Analytics”
How to Drive Manufacturing Sector Growth with Enteros Database Software, AIOps, Cloud FinOps, and RevOps Alignment
Introduction The manufacturing sector is undergoing a profound transformation fueled by Industry 4.0, smart factories, IoT-enabled production lines, and data-driven decision-making. Modern manufacturers are no longer just focused on production—they are building intelligent ecosystems that integrate operations, supply chains, and customer experiences. However, as manufacturing becomes increasingly digitized, organizations face growing challenges. Massive volumes of … Continue reading “How to Drive Manufacturing Sector Growth with Enteros Database Software, AIOps, Cloud FinOps, and RevOps Alignment”