Setting Up PostgreSQL

It's time to get our hands dirty and set up PostgreSQL on your computer! Don't worry if you've never installed database software before; we'll go through this step-by-step.

By the end of this article, you will have PostgreSQL up and running and created your first database.

Installation on Different Operating Systems

PostgreSQL can be installed on various operating systems. We'll cover the most common ones: Windows, macOS, and Linux (Ubuntu).

Windows

  1. Download the installer:

    • Go to the official PostgreSQL website: https://www.postgresql.org/download/windows/
    • Click on the "Download the installer" button
    • Choose the latest version for Windows
  2. Run the installer:

    • Double-click the downloaded file to start the installation process
    • You may be asked for permission to make changes to your computer. Click "Yes"
  3. Installation Wizard:

    • Click "Next" to start
    • Choose your installation directory (the default is usually fine)
    • Select the components you want to install. For beginners, it's safe to keep all components selected
    • Choose a data directory (again, the default is usually fine)
    • Enter a password for the database superuser (remember this password!)
    • Keep the default port (5432) unless you have a specific reason to change it
    • Choose the locale (the default is usually fine)
    • Click "Next" to start the installation
  4. Finish the installation:

    • Once the installation is complete, you can uncheck the box for "Launch Stack Builder" (we won't need it for now)
    • Click "Finish"

PostgreSQL is now installed on your Windows machine.

macOS

On macOS, we'll use Homebrew to install PostgreSQL. If you don't have Homebrew installed, open Terminal and run:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Once Homebrew is installed:

  1. Open Terminal
  2. Run the following command:
    brew install postgresql
    
  3. After installation, start the PostgreSQL service:
    brew services start postgresql
    

That's it!

PostgreSQL is now installed and running on your Mac.

Linux (Ubuntu)

For Ubuntu and other Debian-based distributions:

  1. Open Terminal
  2. Update your system's package list:
    sudo apt update
    
  3. Install PostgreSQL and its contrib package:
    sudo apt install postgresql postgresql-contrib
    
  4. PostgreSQL should start automatically. You can verify this by running:
    sudo systemctl status postgresql
    

You now have PostgreSQL installed on your Ubuntu system!

Basic Configuration

After installation, PostgreSQL is ready to use with its default configuration. However, there are a few things you might want to know or change:

Accessing PostgreSQL

By default, PostgreSQL creates a user named "postgres" during installation. To access PostgreSQL:

  • On Windows: You can use pgAdmin (we'll cover this later) or the psql command-line tool from the Start menu
  • On macOS/Linux: In the terminal, you can switch to the postgres user and then start psql:
sudo -u postgres psql

Changing the Password

It's a good idea to set a strong password for the postgres user. In the psql console:

ALTER USER postgres WITH PASSWORD 'your_new_password';

Replace 'your_new_password' with a strong password of your choice.

These commands won't make sense yet, but we will explore them in the following sections.

Creating a New User

Creating a new user for your projects is often better than using the postgres superuser. In psql:

CREATE USER your_username WITH PASSWORD 'your_password';

Creating a Database

To create a new database:

CREATE DATABASE your_database_name;

To give your new user access to this database:

GRANT ALL PRIVILEGES ON DATABASE your_database_name TO your_username;

Using pgAdmin and psql Command-line Tools

PostgreSQL has two primary tools for managing your databases: pgAdmin (a graphical interface) and psql (a command-line interface).

pgAdmin

pgAdmin is a powerful administration and development platform for PostgreSQL.

To use pgAdmin:

  1. Launch pgAdmin from your start menu (Windows) or Applications folder (macOS)
  2. When you first open pgAdmin, you'll be asked to set a master password. This password protects any saved passwords for database connections
  3. In the browser pane on the left, right-click on "Servers" and choose "Create" > "Server"
  4. Give your server a name (like "Local Server")
  5. In the "Connection" tab:
    • Host name/address: localhost
    • Port: 5432 (default)
    • Maintenance database: postgres
    • Username: postgres (or your created username)
    • Password: The password you set earlier
  6. Click "Save"

You should now see your server in the browser pane. You can explore your databases, tables, and more from here.

[Previous content remains the same]

psql Command-line Tool

psql is PostgreSQL's interactive terminal. It's excellent for quickly executing SQL commands. However, the method to access psql differs between Windows and macOS/Linux.

On Windows:

  1. Open the Start menu
  2. Search for "SQL Shell (psql)" and open it
  3. You'll be prompted to enter the server, database, port, username, and password. You can press Enter to accept the defaults for most of these
  4. For the password, enter the password you set for the postgres user during installation

On macOS:

After installing PostgreSQL via Homebrew:

  1. Open Terminal
  2. To connect to the default 'postgres' database, type:
    psql postgres
    
  3. If you want to connect as the postgres user (which you might need to do for administrative tasks), use:
    psql -U postgres
    
    You might be prompted for your password.

On Linux (Ubuntu):

  1. Open Terminal
  2. Switch to the postgres user:
    sudo -i -u postgres
    
  3. Then start psql:
    psql
    

Once you're in psql, regardless of your operating system, you can use these commands:

  • List all databases: \l
  • Connect to a database: \c database_name
  • List tables in the current database: \dt
  • Describe a table: \d table_name
  • Execute SQL commands (don't forget the semicolon at the end):
    SELECT * FROM your_table;
    
  • Get help: \?
  • Exit psql: \q

The exact steps might vary depending on how PostgreSQL was installed and your system's configuration. If you encounter any issues, check your installation method and consult the PostgreSQL documentation for your specific setup.

In the next article, we'll dive deeper into creating databases and tables, and you'll write your first SQL queries.

BeginnerPostgresqlDatabase
Avatar for Niall Maher

Written by Niall Maher

Founder of Codú - The web developer community! I've worked in nearly every corner of technology businesses: Lead Developer, Software Architect, Product Manager, CTO, and now happily a Founder.

Loading

Fetching comments

Hey! 👋

Got something to say?

or to leave a comment.