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
Balancing the Insurance Sector’s Digital Balance Sheet: How Enteros Uses AIOps and Cloud FinOps to Drive RevOps Efficiency
- 16 September 2025
- Database Performance Management
Introduction The insurance sector stands at a crossroads of tradition and digital transformation. Once reliant on paper records, manual claims processing, and legacy IT systems, insurers today operate in a hyper-connected ecosystem of digital policies, AI-driven underwriting, fraud detection, and customer self-service portals. At the heart of this transformation lies data—massive, complex, and constantly growing. … Continue reading “Balancing the Insurance Sector’s Digital Balance Sheet: How Enteros Uses AIOps and Cloud FinOps to Drive RevOps Efficiency”
Microfinance platforms scaling to millions
- 15 September 2025
- Software Engineering
Introduction Microfinance has transformed financial inclusion, giving underserved communities access to credit and opportunity. But as platforms scale from thousands to millions of borrowers, the very systems enabling this mission can become bottlenecks. The Challenge Peak-hour overload: thousands apply at once, slowing approvals. Read moreMongoDB profiler and database performance problem diagnosis and identificationDelays in scoring: … Continue reading “Microfinance platforms scaling to millions”
Breaking news under load
When traffic spikes become breaking points Election nights. Natural disasters. Global events. In those moments, audiences turn to news sites in record numbers. But just when the newsroom needs to move fastest, the CMS and databases often slow to a crawl. The result: missed updates, frustrated readers, and credibility at risk. When breaking news slows, … Continue reading “Breaking news under load”
Unlocking RevOps Efficiency in the Banking World with AIOps-Powered Database Technology and Root Cause Analysis—Driven by Enteros
Introduction The banking sector has long been a pioneer in adopting cutting-edge technologies to maintain security, efficiency, and customer trust. From mobile banking apps and real-time payments to fraud detection systems and risk management models, financial institutions operate on massive volumes of data and complex database infrastructures. But with this dependency comes a unique set … Continue reading “Unlocking RevOps Efficiency in the Banking World with AIOps-Powered Database Technology and Root Cause Analysis—Driven by Enteros”