1IF NOT EXISTS
If you run 'CREATE TABLE users' twice, the second time it will crash with an error because the table already exists. Best practice is to use 'CREATE TABLE IF NOT EXISTS users'. This makes your script 'Idempotent'—safe to run 100 times without crashing.
2Step-by-Step Breakdown
The Database Server. When you connect to Postgres, you connect to the SERVER. A single server can host hundreds of isolated 'Databases' inside it.
CREATE DATABASE. The very first command you run is to create a container for your project. 'CREATE DATABASE my_app;'. This creates a fresh, empty database.
Switching Databases. If you use the 'psql' terminal, you must 'connect' to your new database using '\c my_app'. In a GUI, you just double-click the new database.
CREATE TABLE. Now you define the structure (Schema). You use 'CREATE TABLE users ( ... )' to define a table and the exact columns it will contain.
Knowledge Check. In a relational database system, what is the hierarchical relationship between a Server, a Database, and a Table?
- →A Server holds Databases; a Database holds Tables
- →A Table holds Databases; a Database holds Servers
Data Types. Every column must have a strict Data Type. Common ones: VARCHAR (text), INT (numbers), BOOLEAN (true/false), TIMESTAMP (dates).
Constraints. You add rules to columns to protect data integrity. 'NOT NULL' means the column cannot be empty. 'UNIQUE' ensures no two users have the same email.
The Primary Key. Every table MUST have a Primary Key. It is a column (usually 'id') that uniquely identifies the row. It is automatically indexed for extreme search speed.
Executing the Script. You highlight your CREATE TABLE script and press Execute. The database verifies the syntax and creates the empty physical file structure on the hard drive.
Summary. Create the DB, Create the Table, Define the Columns, Apply Constraints.
Level Up 🚀
Advanced cheat sheets, SEO tricks, and interview prep for this topic.
Browser Support
Fully supported.
Fully supported.
Fully supported.
Fully supported.
Accessibility (A11y)
1Data Type Reference Tables Need Proper Table Semantics
A tutorial page listing SQL data types (VARCHAR, INT, BOOLEAN, TIMESTAMP) alongside descriptions should use a real HTML <table> with <th> headers, not a series of styled <div> rows, so screen reader users can navigate the type-to-description mapping row by row.
SEO Implications
- 1
Well-Structured Schema Tutorials Rank Better for 'CREATE TABLE' Searches
Content that clearly explains constraints (NOT NULL, UNIQUE, PRIMARY KEY) with real code examples, rather than vague generic prose, tends to match long-tail developer searches like 'postgres create table primary key example' far more precisely.
Best Practices
Use CREATE TABLE IF NOT EXISTS for Idempotent Setup Scripts
A raw CREATE TABLE crashes the second time it runs because the table already exists. Adding IF NOT EXISTS makes the script safe to re-run any number of times without erroring, which matters for setup scripts and migrations.
Always Define an Explicit Primary Key and Relevant NOT NULL/UNIQUE Constraints
A table without a primary key has no reliable way to reference a single row, and skipping NOT NULL/UNIQUE constraints pushes validation entirely onto application code, where it's much easier to forget in one code path and not another.
Frequent Bugs
A setup or seed script crashes with 'relation "users" already exists' the second time it's run.
CREATE TABLE fails if the table is already present. Use CREATE TABLE IF NOT EXISTS so the script is idempotent and safe to run repeatedly, such as during local development or CI.
Real-World Examples
Defining a Users Table With Proper Constraints
A team needed a users table where every row was guaranteed to have a unique ID, a required and unique email, and a creation timestamp.
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
email VARCHAR(100) UNIQUE NOT NULL,
created_at TIMESTAMP DEFAULT NOW()
);