</>
Skip to content
PostgreSQL lessons (10/38)

PostgreSQL — Create Table

Basic Create

CREATE TABLE products (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    price DECIMAL(10, 2),
    quantity INTEGER DEFAULT 0
);

With Constraints

CREATE TABLE orders (
    id SERIAL PRIMARY KEY,
    user_id INTEGER REFERENCES users(id),
    total DECIMAL(10, 2) CHECK (total > 0),
    status VARCHAR(20) DEFAULT 'pending'
);

Create Like

CREATE TABLE products_backup (LIKE products INCLUDING ALL);

Conditional Create

CREATE TABLE IF NOT EXISTS products (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100)
);

Temporary Table

CREATE TEMPORARY TABLE temp_data (
    id INTEGER,
    value TEXT
);

Mini Practice

  1. Create a basic table
  2. Add constraints
  3. Create from existing table
  4. Create temporary table

Up Next

Continue with Data Types — PostgreSQL data types.

Related Topics

Frequently Asked Questions about Create Table

What is Create Table in PostgreSQL?

Create Table is a fundamental concept in PostgreSQL. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Create Table?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Create Table.

Why is Create Table important in PostgreSQL?

Create Table is essential for PostgreSQL development. Understanding this concept will help you write better code and solve real-world problems more effectively.