Navigate back to the homepage

How to setup PostgreSQL on MacOS

Amit Solanki
January 13th, 2019 · 1 min read

I would be lying, if I say I don’t look up how to setup PostgreSQL - every DAMN time. We know it was hard to do before, but now it isn’t, yet most of the guides we find are outdated. So, here we have an updated version of it.

Installing PostgreSQL

First, install HomeBrew if you don’t have it already, you can use the following script to install it.

1/usr/bin/ruby -e "$(curl -fsSL"

Once you are done with it, update HomeBrew dependencies and install PostgreSQL.

1brew update
2brew install postgresql

See, I told you it was Easy. Let’s check Postgres version

1postgres --version
2postgres (PostgreSQL) 11.3

Initializing the DB

A Database needs some space allocated on your hard-disk, also called as “cluster” or “Data directory”. It happens automatically, but to be sure run the following

1initdb /usr/local/var/postgres

You may see the error message: initdb: directory “/usr/local/var/postgres” exists but is not empty if the database was already created when we installed PostgreSQL.

Start/Stop PostgreSQL

You can do it either ways, by directly calling the following

1pg_ctl -D /usr/local/var/postgres start
2pg_ctl -D /usr/local/var/postgres stop

or by using brew

1brew services postgres start
2brew services postgres stop
3brew services postgres restart

Creating a PostgreSQL Database

To create a database run the following

1createdb databasename

To remove a database run the following

1dropdb databasename

To Connect to a database run

1psql databasename

This will open up the psql sheel, where you can run SQL quries, to exit press ctrl+d. Some useful commands, to know.

  • \list To list all databases.
  • \c databaseName to connect to databaseName.
  • \d To list relations in current database.
  • \d tableName To list info of a table.

More articles from Amit Solanki

10 Console tricks, to debug like a Pro.

Some built-in console methods, which can make debugging fun ;)

October 13th, 2018 · 2 min read

ReasonML - Advent of Code - Day 1

Advent Of Code Advent of Code, is an advent calendar to solve small programming puzzles, every day for 25 days. once you solve one puzzle…

December 1st, 2019 · 4 min read
© 2018–2019 Amit Solanki
Link to $ to $