Skip to main content

Postgres Installation

If you already have a local installation of Postgres running on your machine, you can skip this section.

Setting up Postgres on Mac using Brew

This guide will walk you through the process of setting up Postgres on a Macbook using Brew. For other methods please see the Postgres Documentation.

Prerequisites

Before proceeding with this guide, ensure that you have the following prerequisites:

  • A Macbook with MacOS installed
  • A working internet connection
  • Basic knowledge of using the terminal

Installation

  1. Open Terminal on your Macbook.

  2. Install Homebrew package manager by running the following command in your terminal:

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  3. Once Homebrew is installed, use the following command to install Postgres:

    brew install postgresql

This will download and install Postgres and its dependencies.

  1. After installation, start the Postgres service by running the following command:

    brew services start postgresql

This will start the Postgres service and ensure that it starts automatically on boot.

Creating a Default Postgres User and Password

  1. Open Terminal and type the following command to access the Postgres command line interface:

    psql postgres
  2. Once you are in the Postgres command line interface, type the following command to create a new user:

    CREATE USER `enteryourdesiredusernamehere` WITH PASSWORD 'yourpassword';

Replace enteryourdesiredusernamehere and yourpassword with your desired username and password.

  1. Once the user is created, you can grant the user access to a database using the following command:

    GRANT ALL PRIVILEGES ON DATABASE yourdatabasename TO yourusername;

Replace yourdatabasename and yourusername with your desired database name and username.

Congratulations! You have now successfully set up Postgres on your Macbook using Brew and created a default user and password.

Installing Postgres on Windows

Prerequisites

Before proceeding with this guide, ensure that you have the following prerequisites:

  • A Windows computer running Windows 7 or later
  • A working internet connection
  • Administrator access to the computer
  • Basic knowledge of using the command prompt

Installation

  1. Download the Postgres installer from the Postgres website.

  2. Double-click the downloaded installer file to begin the installation process.

  3. Select the components you want to install and click "Next".

  4. Choose the directory where you want to install Postgres and click "Next".

  5. On the "Password" screen, set a password for the postgres user and click "Next".

  6. On the "Port" screen, choose the port number to use for Postgres and click "Next".

  7. On the "Advanced Options" screen, leave the default options as they are, and click "Next".

  8. On the "Ready to Install" screen, click "Next" to start the installation process.

  9. Once the installation process is complete, click "Finish".

  10. Open the Command Prompt and navigate to the directory where Postgres is installed. The default location is C:\Program Files\PostgreSQL\version\bin, where version is the version of Postgres you installed.

  11. Type the following command to start the Postgres service:

    pg_ctl.exe start -D "C:\Program Files\PostgreSQL\version\data"

    This will start the Postgres service.

  12. To verify that Postgres is installed and working correctly, type the following command:

    psql -U postgres

    You will be prompted to enter the password you set for the postgres user during the installation process.

    If everything is working correctly, you should see the Postgres command prompt.

Congratulations! You have now successfully installed Postgres on your Windows computer.

Installing Postgres on Linux

Prerequisites

Before proceeding with this guide, ensure that you have the following prerequisites:

  • A Linux computer running a supported distribution. For a comprehensive list of the supported platforms please see the Postgres docs
  • A working internet connection
  • Administrator access to the computer
  • Basic knowledge of using the command line

Installation

The installation process for Postgres varies depending on your Linux distribution. Here are the installation instructions for some popular distributions:

Ubuntu and Debian

  1. Open a terminal window.

  2. Update the package list by running the following command:

    sudo apt-get update
  3. Install Postgres by running the following command:

    sudo apt-get install postgresql
  4. Once the installation process is complete, the Postgres service should start automatically. You can verify that the service is running by running the following command:

    sudo systemctl status postgresql

CentOS and Fedora

  1. Open a terminal window.

  2. Install the Postgres repository by running the following command:

    sudo dnf install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
  3. Install Postgres by running the following command:

    sudo dnf install postgresql12-server postgresql12
  4. Initialize the database by running the following command:

    sudo /usr/pgsql-12/bin/postgresql-12-setup initdb
  5. Once the initialization process is complete, start the Postgres service by running the following command:

    sudo systemctl start postgresql-12
  6. You can verify that the service is running by running the following command:

    sudo systemctl status postgresql-12

Setting up the Postgres User

  1. Open a terminal window.

  2. Switch to the postgres user by running the following command:

    sudo su - postgres
  3. Once you are logged in as the postgres user, run the following command to access the Postgres command line interface:

    psql
  4. Once you are in the Postgres command line interface, run the following command to set a password for the postgres user:

    \password postgres
  5. Type in the desired password and press Enter.

  6. Once the password is set, exit the Postgres command line interface by running the following command:

    \q

Congratulations! You have now successfully installed Postgres on your Linux computer and set up the postgres user.