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
Transforming Modern Agriculture with Enteros: AI SQL Insights, Cloud FinOps Optimization, and Database Performance Excellence
- 20 October 2025
- Database Performance Management
Introduction The agriculture sector is undergoing a digital transformation driven by data, automation, and artificial intelligence. As agribusinesses, food producers, and supply chain stakeholders embrace technology, the need for high-performing databases and cost-efficient cloud operations has become critical. The rise of AI SQL, Cloud FinOps, and AIOps-powered platforms is reshaping how agricultural organizations process, analyze, … Continue reading “Transforming Modern Agriculture with Enteros: AI SQL Insights, Cloud FinOps Optimization, and Database Performance Excellence”
Revolutionizing the Insurance Sector with Enteros: Database Software, Performance Optimization, and AIOps Platform Excellence
Introduction The insurance sector has always relied heavily on data. From underwriting and risk assessment to claims management and fraud detection, data-driven decision-making is at the core of every insurance operation. However, as the volume, velocity, and variety of data continue to grow exponentially, traditional database systems and performance monitoring tools struggle to keep pace. … Continue reading “Revolutionizing the Insurance Sector with Enteros: Database Software, Performance Optimization, and AIOps Platform Excellence”
Optimizing Financial Intelligence with Enteros: AI SQL and Observability Platform for Smarter Database Performance
- 19 October 2025
- Database Performance Management
Introduction The financial sector thrives on data precision, operational speed, and scalability. With billions of transactions occurring daily across banking, investment, and fintech platforms, even a momentary slowdown can result in missed opportunities, customer dissatisfaction, or compliance risks. As institutions migrate toward AI-driven systems and complex database environments, the demand for smarter performance management has … Continue reading “Optimizing Financial Intelligence with Enteros: AI SQL and Observability Platform for Smarter Database Performance”
Enteros for Smarter Operations: Unifying Healthcare Innovation and AI in Manufacturing Through Database Optimization
Introduction In a world where digital systems and real-time data drive every major decision, both healthcare and manufacturing sectors are undergoing deep technological transformation. From patient monitoring systems to automated assembly lines, the efficiency of operations now depends heavily on the performance and reliability of databases that power mission-critical applications. Yet, with growing complexity in … Continue reading “Enteros for Smarter Operations: Unifying Healthcare Innovation and AI in Manufacturing Through Database Optimization”