Getting started with PostgreSQL on Mac

via Homebrew

Vivi E
3 min readJul 11, 2018

Installation details for this tutorial is specific to macOS. There are other installation options, so make sure to visit PostgreSQL’s website if you are interested.

Step 1. Installation

Install/Update Homebrew

Get brew and check installation details here, if you do not have any. You can also copy-paste the code below on your terminal.

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

In the event that you already have homebrew installed, update it instead by running the following code on your terminal.

brew doctor
brew update

Install PostgreSQL using Homebrew

Get PostgreSQL (psql) by running brew install postgresql on your Terminal. Downloading and installing may take some time because homebrew will also take care of the dependencies needed. Open a new terminal to be able to use PostgreSQL.

Figure 1. Successful installation of PostgreSQL via Homebrew

Step 2. Starting and stopping PostgreSQL

Depending on your settings, you might need to restart PostgreSQL every time you restart your machine. But I will suggest you to put the effort of stopping, and staring it manually to save precious computing resources.

Start PostgreSQL

To start using database services, run the code below on the terminal

brew services start postgresql

At first run, homebrew might need to install some more dependencies. No need to worry about this because this will be triggered automatically.

Figure 2. Successful starting of PostgreSQL using brew services

Stop PostgreSQL

To stop using database services, run the code below on the terminal

brew services stop postgresql
Figure 3. Successful stopping of PostgreSQL using brew services

Step 3. Start using PostgreSQL

Start PostgreSQL terminal (root privileges)

brew services start postgresql
psql postgres
Figure 4. psql terminal with root privileges

Create a new user and assign roles

Inside the psql terminal, I suggest you create a new user with controlled roles.

# create a new user inside the psql terminal
# password must be enclosed with quotes
CREATE ROLE newuser WITH LOGIN PASSWORD 'password';
# make the newuser capable of creating, editing, and deleting databases
ALTER ROLE newuser CREATEDB;
# Quit psql terminal to be able to login using newuser
\q
# Go back to psql terminal, with `newuser` as user
psql postgres -U newuser
# Observe that from `postgres=#`, the psql terminal instead shows `postgres=>`
Figure 5. New user with create database roles

Quit PostgreSQL terminal

To get out of the psql terminal, enter \q.

Explore more commands

Check out the documentation page of the psql version that you downloaded for more database commands.

Errors

I decided to add a section where I solved the errors encountered along the way.

`psql` on Terminal

To get to the PostgreSQL terminal, open your terminal, start the database services (brew services start postgresql), then run psql.

This might produce an error saying psql: FATAL: database "<USER>" does not exist. I solved this by creating a database named after <USER> from the terminal (createdb <USER>)

Figure 6. psql terminal

Thank you!

Sources

--

--