Initial Setup
Get PostgreSQL installed and execute your first queries. This guide walks you through installation on any operating system and basic database operations.
π― What You’ll Accomplish
By the end of this tutorial, you’ll have:
- β PostgreSQL installed and running
- β psql command-line tool configured
- β Your first database created
- β Basic SQL queries executed
π Prerequisites
- Basic familiarity with your computer’s terminal/command line
- No prior PostgreSQL or database experience required
πΎ Step 1: Install PostgreSQL
Windows
Download the installer from postgresql.org:
- Run the installer
- Choose installation directory
- Select components (PostgreSQL Server, pgAdmin, Command Line Tools)
- Set password for postgres superuser (remember this!)
- Set port (default: 5432)
- Choose locale
- Complete installation
macOS
Using Homebrew:
brew install postgresql@16
brew services start postgresql@16Or download Postgres.app for a GUI experience.
Linux (Ubuntu/Debian)
sudo apt update
sudo apt install postgresql postgresql-contrib
sudo systemctl start postgresql
sudo systemctl enable postgresqlβ Step 2: Verify Installation
psql --versionExpected output:
psql (PostgreSQL) 16.xπ Step 3: Access PostgreSQL
Linux/macOS
sudo -u postgres psqlWindows
Open “SQL Shell (psql)” from Start menu.
Create Your User (Linux/macOS)
CREATE USER yourname WITH PASSWORD 'yourpassword';
ALTER USER yourname CREATEDB;
\qThen connect as your user:
psql -U yourname -d postgresποΈ Step 4: Create Your First Database
CREATE DATABASE myapp;
\lConnect to your database:
\c myappπ Step 5: Create Your First Table
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);Verify table creation:
\dt
\d usersπ Step 6: Insert and Query Data
Insert data:
INSERT INTO users (name, email) VALUES
('Alice Johnson', 'alice@example.com'),
('Bob Smith', 'bob@example.com'),
('Charlie Brown', 'charlie@example.com');Query data:
SELECT * FROM users;
SELECT name, email FROM users WHERE name LIKE 'A%';
SELECT COUNT(*) FROM users;β Verification Checklist
Before moving forward, verify:
-
psql --versionshows PostgreSQL installed - You can connect to PostgreSQL with psql
- You created a database successfully
- You can create tables and insert data
- You can query data with SELECT
π You’re Done!
You’ve successfully installed PostgreSQL and executed your first queries. You’re ready to learn more advanced database operations.
π What’s Next?
Quick learner: PostgreSQL Quick Start
- Learn essential SQL operations and PostgreSQL features
- Understand enough to work with databases independently
Code-first learner: PostgreSQL By Example
Practical examples covering common database operations
Best for learning through real-world scenarios
Progressive learning path from beginner to advanced
Deep understanding of PostgreSQL capabilities
π Troubleshooting
Connection Issues
Problem: “psql: error: connection to server failed”
Solution: Verify PostgreSQL service is running:
# macOS
brew services list
# Linux
sudo systemctl status postgresql
# Windows
# Check Services app for "postgresql-x64-16"Authentication Failed
Problem: “psql: FATAL: password authentication failed”
Solution: Reset postgres password:
# Linux/macOS
sudo -u postgres psql
ALTER USER postgres WITH PASSWORD 'newpassword';Port Already in Use
Problem: “Port 5432 is already in use”
Solution: Either stop the existing PostgreSQL instance or configure a different port in postgresql.conf.