DEV Community

Hardik Kanajariya
Hardik Kanajariya

Posted on

Day 2: Installing PostgreSQL and Setting Up Your Environment

Day 2: Installing PostgreSQL and Setting Up Your Environment

Welcome back! Today, we'll install PostgreSQL on your system and set up the essential tools you'll need.

Installation Options

Windows Installation

  1. Download the Installer

  2. Run the Installer

    • Double-click the downloaded .exe file
    • Follow the installation wizard
    • Set a strong password for the postgres superuser (SAVE THIS!)
    • Default port: 5432 (keep it unless you have conflicts)
  3. Install pgAdmin 4

    • Typically included in the installer
    • pgAdmin is a visual management tool for PostgreSQL

macOS Installation

Using Homebrew (Recommended)

# Install Homebrew if you haven't
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# Install PostgreSQL
brew install postgresql@16

# Start PostgreSQL service
brew services start postgresql@16
Enter fullscreen mode Exit fullscreen mode

Or download from postgresql.org

Linux (Ubuntu/Debian) Installation

# Update package list
sudo apt update

# Install PostgreSQL
sudo apt install postgresql postgresql-contrib

# Start the service
sudo systemctl start postgresql
sudo systemctl enable postgresql
Enter fullscreen mode Exit fullscreen mode

Verifying Your Installation

Open your terminal/command prompt:

# Check PostgreSQL version
psql --version

# Should output something like:
# psql (PostgreSQL) 16.x
Enter fullscreen mode Exit fullscreen mode

First Connection

Using Command Line

Windows:

psql -U postgres
Enter fullscreen mode Exit fullscreen mode

macOS/Linux:

sudo -u postgres psql
Enter fullscreen mode Exit fullscreen mode

You should see:

postgres=#
Enter fullscreen mode Exit fullscreen mode

Congratulations! You're connected! ๐ŸŽ‰

Installing pgAdmin 4

pgAdmin is a powerful graphical interface for PostgreSQL.

  1. Download from pgadmin.org
  2. Install and launch pgAdmin
  3. Create a new server connection:
    • Right-click "Servers" โ†’ Register โ†’ Server
    • Name: "Local PostgreSQL"
    • Host: localhost
    • Port: 5432
    • Username: postgres
    • Password: (the one you set during installation)

Basic Terminal Commands

-- List all databases
\l

-- Connect to a database
\c database_name

-- List all tables
\dt

-- Quit psql
\q
Enter fullscreen mode Exit fullscreen mode

Troubleshooting Common Issues

"psql: command not found"

  • Add PostgreSQL to your PATH environment variable
  • Windows: Add C:\Program Files\PostgreSQL\16\bin to PATH

Connection refused errors

  • Ensure PostgreSQL service is running
  • Check if port 5432 is available

Permission denied

  • Make sure you're using the correct username and password

Your First SQL Command

Let's test everything works:

SELECT version();
Enter fullscreen mode Exit fullscreen mode

This should display your PostgreSQL version information.


Tomorrow's Preview: Day 3 - Creating Your First Database and Understanding Database Structure

Homework:

  • Install PostgreSQL successfully
  • Connect via both command line and pgAdmin
  • Run the SELECT version(); command

Share your PostgreSQL version in the comments! ๐Ÿš€

Top comments (0)