Skip to content

PostgreSQL is not only a robust open-source object-relational database system but also an essential tool for developers looking to manage complex data workloads efficiently. Understanding how to create and manage tables in PostgreSQL is crucial for any database professional. Therefore, to equip you with these essential skills, this guide will delve into the process of creating and managing tables in PostgreSQL.

How to create a table in PostgreSQL?

Use the CREATE TABLE statement to establish a new table in PostgreSQL. Here’s an example that constructs an employees table:

CREATE TABLE employees (
    id SERIAL PRIMARY KEY,
    first_name VARCHAR(50),
    last_name VARCHAR(50),
    email VARCHAR(100),
    hire_date DATE
);

This script sets up a table where id acts as an auto-incrementing primary key and defines several other fields for employee information.

How to add columns in PostgreSQL?

Add new columns to an existing table with the ALTER TABLE command followed by ADD COLUMN. To add a department column to the employees table, execute:

ALTER TABLE employees ADD COLUMN department VARCHAR(50);

How to modify columns in PostgreSQL?

Change a column’s type or modify its attributes using ALTER TABLE followed by ALTER COLUMN. For example, expand the department column’s capacity with:

ALTER TABLE employees ALTER COLUMN department TYPE VARCHAR(100);

How to delete columns in PostgreSQL?

Remove an unwanted column from a table with ALTER TABLE followed by DROP COLUMN. Note that this action is irreversible:

ALTER TABLE employees DROP COLUMN department;

How to insert data into a table?

Insert data into your table using the INSERT INTO statement:

INSERT INTO employees (first_name, last_name, email, hire_date)
VALUES ('John', 'Doe', '[email protected]', '2023-01-01');

How to query data?

Retrieve data from your table with the SELECT statement. To fetch all records from the employees table:

SELECT * FROM employees;

For specific columns, use:

SELECT first_name, last_name FROM employees;

Properly managing tables is key to leveraging the full potential of PostgreSQL. By adopting these commands, you’ll streamline your data processes, ensuring efficient and organized data management.

Written by

Robert Cooper avatar

Robert Cooper

Senior Engineer at Basedash

Robert Cooper is a senior engineer at Basedash who builds full-stack product systems across SQL data infrastructure, APIs, and frontend architecture. His work focuses on application performance, developer velocity, and reliable self-hosted workflows that make data operations easier for teams at scale.

View full author profile →

Looking for an AI-native BI tool?

Basedash lets you build charts, dashboards, and reports in seconds using all your data.