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
Intelligent Healthcare Performance Management: Enteros’ AIOps and Cloud FinOps Framework
- 18 December 2025
- Database Performance Management
Introduction Healthcare organizations are under unprecedented pressure to deliver better patient outcomes while managing rising operational costs, increasing regulatory demands, and rapidly expanding digital infrastructure. From electronic health records (EHRs) and telemedicine platforms to clinical analytics, revenue cycle management systems, and AI-assisted diagnostics, modern healthcare relies on highly complex, data-driven technology ecosystems. As these systems … Continue reading “Intelligent Healthcare Performance Management: Enteros’ AIOps and Cloud FinOps Framework”
How Enteros Transforms Retail Performance Management with AI-Driven Cost Estimation
Introduction The retail industry is operating in one of the most demanding digital environments in history. Omnichannel commerce, real-time inventory visibility, hyper-personalized customer journeys, dynamic pricing, and always-on digital storefronts have become non-negotiable expectations. Behind these seamless experiences lies a highly complex IT ecosystem powered by cloud platforms, SaaS databases, analytics engines, and microservices architectures. … Continue reading “How Enteros Transforms Retail Performance Management with AI-Driven Cost Estimation”
Driving Retail Excellence: How Enteros Delivers Intelligent Performance Management for SaaS Database Environments
- 17 December 2025
- Database Performance Management
Introduction The retail industry is evolving faster than any other sector in the digital economy. Omnichannel commerce, real-time inventory visibility, hyper-personalized customer experiences, subscription-based business models, and AI-powered analytics have become table stakes for modern retailers. Behind all of this innovation lies a complex web of SaaS applications, cloud-native platforms, and high-performance databases. Retailers now … Continue reading “Driving Retail Excellence: How Enteros Delivers Intelligent Performance Management for SaaS Database Environments”
Driving Healthcare Growth with Enteros: Optimizing Database Performance and Cloud FinOps for High-Impact Technology Operations
Introduction Healthcare organizations are experiencing a historic shift toward digital-first operations. Electronic Health Records (EHRs), telemedicine platforms, AI-powered diagnostics, patient engagement applications, research databases, and real-time analytics systems are now foundational to modern care delivery. At the heart of this transformation lies one critical dependency: high-performing, cost-efficient, and reliable databases operating at scale. However, as … Continue reading “Driving Healthcare Growth with Enteros: Optimizing Database Performance and Cloud FinOps for High-Impact Technology Operations”