CREATE TABLE

Updated · 9 min read
easyPostgreSQL 18sqlcreate-tableddltemporary-tablenaming

CREATE TABLE builds a new, empty table: you name it and list its columns, each with a data type. So far you've worked with the Fee Tracker's five ready-made tables. Now you'll make your own. Navika wants to log her study time: which student studied, on which day, for how many minutes, and on what topic. That's a new table, study_sessions. This lesson covers writing CREATE TABLE, checking what you built, what the types already protect, IF NOT EXISTS, temporary tables, and naming things so you never need quotes.

In One Minute

  • CREATE TABLE name (column type, column type, ...); makes an empty table.
  • Every column has a name and a data type; the type rejects values that don't fit.
  • CREATE TABLE IF NOT EXISTS skips the step quietly when the table is already there.
  • CREATE TEMPORARY TABLE makes a table that disappears when your session ends.
  • Use lowercase names with underscores (study_sessions); then you never need quotes.

The Basics

SQL
CREATE TABLE table_name (
  column_name data_type,
  column_name data_type,
  ...
);

Navika's study log:

SQL▶ Try it
CREATE TABLE study_sessions (
  session_id INTEGER,
  student_id INTEGER,
  studied_on DATE,
  minutes    INTEGER,
  topic      VARCHAR(60)
);
 
INSERT INTO study_sessions
VALUES (1, 103, '2026-01-05', 45, 'JavaScript loops');
 
SELECT * FROM study_sessions;
Output
CREATE TABLE
 
INSERT 0 1
 
 session_id | student_id | studied_on | minutes |      topic
------------+------------+------------+---------+------------------
          1 |        103 | 2026-01-05 |      45 | JavaScript loops
(1 row)

CREATE TABLE answered with its tag, and the table was ready for the INSERT right away. The columns come out in the order you listed them. A few details of the syntax:

  • The column list is in parentheses, with a comma between columns and none after the last one.
  • Lining up the types in a column is only for readability; spacing doesn't matter to SQL.
  • VARCHAR(60) means text of at most 60 characters. SQL Data Types, next, covers every type.
Each line of the column list becomes one column of the new table

Checking What You Built

Every PostgreSQL database describes its own tables in information_schema, a set of read-only views you can query like any table. The columns of study_sessions:

SQL▶ Try it
CREATE TABLE study_sessions (
  session_id INTEGER,
  student_id INTEGER,
  studied_on DATE,
  minutes    INTEGER,
  topic      VARCHAR(60)
);
 
SELECT column_name, data_type, character_maximum_length
FROM information_schema.columns
WHERE table_name = 'study_sessions'
ORDER BY ordinal_position;
Output
CREATE TABLE
 
 column_name |     data_type     | character_maximum_length
-------------+-------------------+--------------------------
 session_id  | integer           |                     NULL
 student_id  | integer           |                     NULL
 studied_on  | date              |                     NULL
 minutes     | integer           |                     NULL
 topic       | character varying |                       60
(5 rows)

VARCHAR shows by its full standard name, character varying, with its limit of 60. information_schema is part of the SQL standard, so the same query works in MySQL and SQL Server too. In psql, \d study_sessions shows the same thing in a shorter form (PostgreSQL only).

What the Types Already Protect

Data types are the first rule a table enforces. A date column refuses text that isn't a date:

SQL▶ Try it
CREATE TABLE study_sessions (
  session_id INTEGER,
  studied_on DATE
);
 
INSERT INTO study_sessions VALUES (1, 'yesterday-ish');
Output
CREATE TABLE
 
ERROR:  invalid input syntax for type date: "yesterday-ish"

And VARCHAR(10) refuses text longer than 10 characters, instead of cutting it short:

SQL▶ Try it
CREATE TABLE study_sessions (
  minutes INTEGER,
  topic   VARCHAR(10)
);
 
INSERT INTO study_sessions VALUES (45, 'JavaScript loops');
Output
CREATE TABLE
 
ERROR:  value too long for type character varying(10)

But types alone don't know your business rules. This table happily accepts a student who doesn't exist (999), a negative number of minutes and a missing topic:

SQL▶ Try it
CREATE TABLE study_sessions (
  session_id INTEGER,
  student_id INTEGER,
  studied_on DATE,
  minutes    INTEGER,
  topic      VARCHAR(60)
);
 
INSERT INTO study_sessions VALUES (1, 999, '2026-01-05', -45, NULL);
 
SELECT * FROM study_sessions;
Output
CREATE TABLE
 
INSERT 0 1
 
 session_id | student_id | studied_on | minutes | topic
------------+------------+------------+---------+-------
          1 |        999 | 2026-01-05 |     -45 | NULL
(1 row)
Types reject a bad date and a too-long topic, but accept student 999 and minus 45 minutes

IF NOT EXISTS

Creating a table that already exists is an error:

SQL▶ Try it
CREATE TABLE courses (
  course_id INTEGER
);
Output
ERROR:  relation "courses" already exists

CREATE TABLE IF NOT EXISTS skips the step instead, which suits setup scripts that may run twice. It prints a NOTICE instead of an error, then the usual tag. Be careful: it checks only the name. The existing table is left exactly as it was, even when its columns are completely different from what you wrote:

SQL▶ Try it
CREATE TABLE IF NOT EXISTS courses (
  x INTEGER
);
 
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'courses'
ORDER BY ordinal_position;
Output
NOTICE:  relation "courses" already exists, skipping
CREATE TABLE
 
   column_name
-----------------
 course_id
 title
 technology
 level
 fee_usd
 prerequisite_id
(6 rows)

courses still has its six real columns, not a column called x. MySQL and SQLite support IF NOT EXISTS too; SQL Server has no CREATE TABLE IF NOT EXISTS, so scripts there check first with IF OBJECT_ID('study_sessions') IS NULL (SQL Server only).

Temporary Tables

A temporary table lives only until your session ends, then PostgreSQL drops it for you. It's handy for scratch work, such as holding today's notes while you build a report:

SQL▶ Try it
CREATE TEMPORARY TABLE todays_notes (
  student_id INTEGER,
  note       TEXT
);
 
INSERT INTO todays_notes VALUES (103, 'Finished loops quiz');
 
SELECT * FROM todays_notes;
Output
CREATE TABLE
 
INSERT 0 1
 
 student_id |        note
------------+---------------------
        103 | Finished loops quiz
(1 row)

Other sessions can't see it, even while it exists, so two people can each have their own todays_notes without clashing. (In Try it every run is its own session, so every table you create there is short-lived anyway.) SQL Server marks temporary tables with a # at the start of the name instead, as in #todays_notes (SQL Server only).

Naming Tables and Columns

PostgreSQL folds unquoted names to lowercase. CREATE TABLE Study_Sessions creates a table named study_sessions. Quoting a name with double quotes keeps it exactly, spaces and capitals included, but then you must quote it the same way every time:

SQL▶ Try it
CREATE TABLE "Study Sessions" (
  id INTEGER
);
 
SELECT * FROM "study sessions";
Output
CREATE TABLE
 
ERROR:  relation "study sessions" does not exist

The quoted name is Study Sessions with capitals, so "study sessions" doesn't match. Reserved words such as order and user fail without quotes too (syntax error at or near "order").

Study_Sessions becomes study_sessions; "Study Sessions" must be written with the same quotes every time

Most teams also follow these habits, as the Fee Tracker does:

HabitFee Tracker example
Plural table namesstudents, payments
table_id for the identifierstudent_id, payment_id
The unit in the nameamount_usd, minutes
_on for dates, _at for timestampspaid_on, joined_on

Common Mistakes

A comma after the last column. topic VARCHAR(60), followed by ) is a syntax error. Commas go between columns.

Mixed-case or spaced names. "Study Sessions" forces quotes forever. Use study_sessions.

Trusting IF NOT EXISTS to update a table. It checks only the name and leaves an existing table untouched.

Relying on types alone. Types reject a bad date, not a negative duration. Add constraints.

Practice

Challenge: Create a table practice_goals with a goal_id whole number, a student_id whole number, a goal of up to 80 characters, and a due_on date. Insert one goal for Navika (student 103): "Finish JavaScript Basics" by 2026-02-15. Then show the table.

Show solution
SQL▶ Try it
CREATE TABLE practice_goals (
  goal_id    INTEGER,
  student_id INTEGER,
  goal       VARCHAR(80),
  due_on     DATE
);
 
INSERT INTO practice_goals
VALUES (1, 103, 'Finish JavaScript Basics', '2026-02-15');
 
SELECT * FROM practice_goals;
Output
CREATE TABLE
 
INSERT 0 1
 
 goal_id | student_id |           goal           |   due_on
---------+------------+--------------------------+------------
       1 |        103 | Finish JavaScript Basics | 2026-02-15
(1 row)

Four columns, in the order you listed them, and one row. Next you'll choose types more carefully, in SQL Data Types.

Quick check: CREATE TABLE

3 questions
1What does CREATE TABLE study_sessions (...); create?
2You run CREATE TABLE Study_Sessions (id INTEGER); in PostgreSQL. What is the table's name?
3courses already exists. What does CREATE TABLE IF NOT EXISTS courses (x INTEGER); do?

Key Takeaways

You want toWrite
Make a tableCREATE TABLE study_sessions (session_id INTEGER, ...);
Skip if it existsCREATE TABLE IF NOT EXISTS study_sessions (...);
A scratch tableCREATE TEMPORARY TABLE todays_notes (...);
See its columnsSELECT column_name, data_type FROM information_schema.columns WHERE table_name = 'study_sessions';

Was this tutorial helpful?

Lesson 28 of 115