How to create a table in PostgreSQL

Creating a table in PostgreSQL involves using the CREATE TABLE SQL command, which allows you to define the table's structure by specifying its columns, each with a specific data type and constraints if necessary. This article delves into the basics of creating a table and progresses to more advanced use cases, including the variety of data types available in PostgreSQL.

Basic Command Structure

The basic syntax for creating a table in PostgreSQL is as follows:


CREATE TABLE table_name (
    column_name1 data_type constraints,
    column_name2 data_type constraints,
    ...
);

In this syntax:

  • table_name is the name you wish to assign to your table.

  • column_name is the name of a column in your table.

  • data_type specifies what kind of data the column can hold (e.g., integer, text).

  • constraints are optional rules applied to column data (e.g., NOT NULL, UNIQUE).

Example: Creating a Simple Table

Let's create a simple table named employees:


CREATE TABLE employees (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100),
    department VARCHAR(50),
    salary NUMERIC(10, 2),
    hire_date DATE DEFAULT CURRENT_DATE
);

This table includes columns for an employee ID, name, department, salary, and hire date. The SERIAL keyword for the id column automatically increments the value, making it useful for a primary key.

Advanced Use Cases

Specifying Default Values

You can set default values for columns using the DEFAULT keyword. For instance, if you want every new employee to default to a specific department, you could modify the department column definition:


department VARCHAR(50) DEFAULT 'General'

Using Constraints

Constraints are rules that the data in a table must follow. PostgreSQL supports several constraints such as NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, and CHECK. For example, to ensure no two employees have the same email address, you could add a UNIQUE constraint to an email column:


email VARCHAR(255) UNIQUE

Data Types in PostgreSQL

PostgreSQL supports a wide range of data types. Here's a table summarizing some of the most common types:

Data TypeDescription
INTEGERA typical integer
SERIALAuto-incrementing integer, useful for primary keys
VARCHAR(n)Variable-length character string, maximum length n
TEXTUnlimited length text
NUMERIC(p, s)Numeric values with a specified precision (p) and scale (s)
DATEDate (no time of day)
TIMESTAMPDate and time
BOOLEANTrue/false values
JSONJSON data - preserves original text structure
JSONBJSON data in binary format - faster to query
ARRAYAn array of values, can be used with other data types

This table is not exhaustive, but it covers the most commonly used data types in PostgreSQL. Choosing the right data type for your columns is crucial for performance, storage efficiency, and data integrity.

In summary, creating a table in PostgreSQL involves defining its structure through the CREATE TABLE command, specifying columns, their data types, and any constraints. As your database schema evolves, you may find yourself using more advanced features and data types to meet your application's needs.