Get PostgreSQL running on your machine and create your first database in under 15 minutes. This guide covers installation, basic SQL operations, and essential database management.
Prerequisites
-
Operating System macOS 12+, Ubuntu 20.04+, or Windows 10/11
-
Disk Space At least 500MB free
-
Admin Access sudo/administrator privileges required for installation
Install PostgreSQL
Install PostgreSQL
~3 minDownload and install PostgreSQL for your platform:
macOS (using Homebrew):
brew install postgresql@16
After installation, follow the instructions to add PostgreSQL to your PATH.
Linux (Ubuntu/Debian):
# Add PostgreSQL APT repository
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
Import repository signing key
wget –quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
Update and install
sudo apt-get update
sudo apt-get install postgresql-16
Windows:
winget install PostgreSQL.PostgreSQL
The Windows installer will prompt you to set a password for the postgres user. Remember this password!
Start the Server
Start the Server
~1 minStart the PostgreSQL server:
macOS:
brew services start postgresql@16
==> Successfully started `postgresql@16` (label: homebrew.mxcl.postgresql@16)
Linux:
sudo systemctl start postgresql
sudo systemctl enable postgresql
Windows:
net start postgresql-x64-16
On Windows, the service usually starts automatically after installation.
Create a Database
Create a Database
~1 minCreate a new database for your project:
macOS/Linux:
createdb myapp
Windows (or if you need to specify a user):
createdb -U postgres myapp
On Linux, you may need to run as the postgres user: sudo -u postgres createdb myapp
Verify the database was created:
psql -l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+---------+-------+-------------------
myapp | youruser | UTF8 | C | C |
postgres | youruser | UTF8 | C | C |
template0 | youruser | UTF8 | C | C | =c/youruser
template1 | youruser | UTF8 | C | C | =c/youruser
Connect with psql
Connect with psql
~1 minConnect to your new database using the psql command-line client:
psql myapp
psql (16.2)
Type "help" for help.
myapp=#
You're now in the psql interactive terminal. The # prompt indicates superuser access.
Useful psql commands: \q to quit, \dt to list tables, \d tablename to describe a table.
Create a Table
Create a Table
~2 minCreate a users table to store some data:
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE
Verify the table was created:
\d users
Table "public.users"
Column | Type | Collation | Nullable | Default
------------+-----------------------------+-----------+----------+-----------------------------------
id | integer | | not null | nextval('users_id_seq'::regclass)
name | character varying(100) | | not null |
email | character varying(255) | | not null |
created_at | timestamp without time zone | | | CURRENT_TIMESTAMP
Indexes:
"users_pkey" PRIMARY KEY, btree (id)
"users_email_key" UNIQUE CONSTRAINT, btree (email)
SERIAL creates an auto-incrementing integer. PostgreSQL automatically creates a sequence for this.
Insert Data
Insert Data
~2 minInsert some sample data into the users table:
INSERT INTO users (name, email) VALUES
('Alice Johnson', '[email protected]'),
('Bob Smith', '[email protected]'),
('Carol Williams', '[email protected]');
INSERT 0 3
Insert a single row and return the generated ID:
INSERT INTO users (name, email)
VALUES ('David Brown', '[email protected]')
RETURNING id, name;
id | name
----+-------------
4 | David Brown
(1 row)
INSERT 0 1
RETURNING is a PostgreSQL-specific feature that returns data from inserted rows.
Query Data
Query Data
~2 minRetrieve all users from the table:
SELECT * FROM users;
id | name | email | created_at
----+----------------+--------------------+----------------------------
1 | Alice Johnson | [email protected] | 2026-02-03 10:30:00.123456
2 | Bob Smith | [email protected] | 2026-02-03 10:30:00.123456
3 | Carol Williams | [email protected] | 2026-02-03 10:30:00.123456
4 | David Brown | [email protected] | 2026-02-03 10:31:15.654321
(4 rows)
Query with conditions and ordering:
SELECT id, name, email
FROM users
WHERE name LIKE '%Smith%'
ORDER BY created_at DESC;
id | name | email
----+-----------+-----------------
2 | Bob Smith | [email protected]
(1 row)
You've successfully created a table, inserted data, and run queries.
Basic Operations (UPDATE, DELETE)
Basic Operations (UPDATE, DELETE)
~3 minUpdate a user’s email address:
UPDATE users
SET email = '[email protected]'
WHERE id = 1
RETURNING *;
id | name | email | created_at
----+---------------+---------------------------+----------------------------
1 | Alice Johnson | [email protected] | 2026-02-03 10:30:00.123456
(1 row)
UPDATE 1
Delete a user:
DELETE FROM users WHERE id = 4;
DELETE 1
Always use a WHERE clause with UPDATE and DELETE to avoid modifying all rows!
Count remaining users:
SELECT COUNT(*) FROM users;
count
-------
3
(1 row)
Exit psql when you’re done:
\q
You've installed PostgreSQL, created a database and table, and performed CRUD operations. You're ready to build applications!