Skip to content

PostgreSQL Installation

Kotz edited this page Dec 28, 2021 · 2 revisions

This guide explains how to setup PostgreSQL on your machine. This is a one-time procedure, if you already have PostgreSQL installed on your machine, do not follow these steps a second time.

Follow the guide for your corresponding operational system.

Windows

  • Download the latest version of PostgreSQL.
  • Run the installer. During installation, you will be asked to define a password for the postgres user. Save the password somewhere safe, we will need it later.
    • There is no need to run the StackBuilder when the installation finishes.
  • Search for psql on Windows, execute the first result (should be called SQL Shell (psql)).
  • A terminal window should open. Hit Enter until it asks for the password for the postgres user. Then enter the password (nothing will show up while you're typing it, that's normal) and hit Enter.
  • You should be logged into PostgreSQL's shell. It looks like this.
  • PostgreSQL's Shell
  • Let's now create a PostgreSQL user. Type in the following command:
    • CREATE ROLE username LOGIN CREATEDB ENCRYPTED PASSWORD 'my_password';
    • Replace username with the actual name you want to use.
    • Replace my_password with the actual password you want to use for that user.
    • Remember both of these as the bot will use these credentials to create and access its database.
    • When you're ready, hit Enter to execute the command.
  • Type in quit to exit the shell.
  • That's it. You're done setting up PostgreSQL.

Linux

  • Follow the most appropriate instructions for your distro.
  • Once installation is done, type in the following command:
    • sudo -u postgres psql -c "CREATE ROLE $(whoami) LOGIN CREATEDB ENCRYPTED PASSWORD 'my_password';"
    • Replace $(whoami) with the actual name you want to use. If you leave it there, it defaults to your local username.
    • Replace my_password with the actual password you want to use for that user.
    • Remember both of these as the bot will use these credentials to create and access its database.
    • When you're ready, hit Enter to execute the command.
  • That's it. You're done setting up PostgreSQL.

MacOS

There are two ways of installing PostgreSQL on a Macintosh, the first one being the preferable way. Use the second way if the first one doesn't work for any reason.

Installing PostgreSQL with a package manager (Homebrew)

  • Type in "terminal" on Spotlight, open the first result.
  • If you don't already have Homebrew, you need to install it by running the following command in the terminal:
    • /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    • Input your user password and hit Enter. When it asks for confirmation, hit Enter again.
    • Wait for it to finish installing (it takes a while).
      • (Optional) Once it finishes installing, install Cakebrew by running brew install cakebrew --cask to have an easy way to manage your brew packages.
  • Now we will use Homebrew to install PostgreSQL. Run the following command:
    • brew install postgresql
  • Then make PostgreSQL start automatically when you turn on your computer:
    • brew services start postgresql
  • By default, Homebrew sets up the PostgreSQL's superuser to the same name as your current user account. This is fine, but may cause issues with most online guides since most of them assume your superuser is called postgres. Let's remedy this by creating the user with the following command:
    • psql -d postgres -U $(whoami) -c "CREATE ROLE postgres WITH LOGIN SUPERUSER CREATEDB CREATEROLE REPLICATION BYPASSRLS ENCRYPTED PASSWORD 'my_password';"
    • Replace my_password above with the actual password you want for the superuser.
  • Now lets "downgrade" the role that Homebrew created for us:
    • psql -U postgres -c "ALTER ROLE $(whoami) WITH LOGIN NOSUPERUSER NOCREATEROLE NOREPLICATION NOBYPASSRLS ENCRYPTED PASSWORD 'my_password';"
    • Replace my_password above with the actual password you want.
  • That's it. You're done setting up PostgreSQL.

Installing PostgreSQL manually

  • Download the latest version of PostgreSQL.
  • Mount the .dmg file and run the installer (double-click it).
  • Go through the setup wizard. During installation, you will be asked to define a password for the postgres user. Save the password somewhere safe, we will need it later.
    • There is no need to run the StackBuilder when the installation finishes.
  • Search for psql on Spotlight, execute the first result (should be called SQL Shell (psql)).
  • A terminal window should open. Hit Enter until it asks for the password for the postgres user. Then enter the password (nothing will show up while you're typing it, that's normal) and hit Enter.
  • You should be logged into PostgreSQL's shell. It looks like this.
  • PostgreSQL's Shell
  • Let's now create a PostgreSQL user. Type in the following command:
    • CREATE ROLE username LOGIN CREATEDB ENCRYPTED PASSWORD 'my_password';
    • Replace username with the actual name you want to use.
    • Replace my_password with the actual password you want to use for that user.
    • Remember both of these as the bot will use these credentials to create and access its database.
    • When you're ready, hit Enter to execute the command.
  • Type in quit to exit the shell.
  • That's it. You're done setting up PostgreSQL.

Clone this wiki locally