Installing PostgreSQL on a Mac with Homebrew

Installing PostgreSQL on a Mac with Homebrew

Β·

3 min read

Suppose you had a look at the 2021 stack overflow developer survey results. In that case, you might have seen that PostgreSQL, or Postgres for short, is now the second most loved database.

And I've been loving Postgres myself, so time to dedicate an article on how to set it up on a Mac. This article will guide you through installing Postgres on your Mac and exploring the first database.

Stack overflow most loved databases 2021

In this article, I'll be using Homebrew. If you haven't used it before, check out my article on using Homebrew.

Installing Postgresql with Homebrew

The first thing we want to do is install Postgres.

Before running any install command in Homebrew, it's good to make sure you are up to date.

brew update

Now it's time to install Postgres, and you can run this command.

brew install postgresql

Once it's done, you should see this output line amongst some other stuff.

This formula has created a default database cluster with:
  initdb --locale=C -E UTF-8 /opt/homebrew/var/postgres

That means you are set!

Managing the Postgres database

To start the database, you can simply run the following command in your terminal:

brew services start postgresql

And to stop the Postgres database from running:

brew services stop postgresql

Once it's up and running, we want to create a root user to log in and interact with the database.

psql postgres

This will log you into the Postgres server.

Postgres server login

From here, you can create a new user with a password.

CREATE ROLE chris WITH LOGIN PASSWORD 'password';
ALTER ROLE chris CREATEDB;

My user's name here is chris, and my password is super secure as it is: password.

Note: Use secure passwords, please this is just a demo setup.

Connecting to the Postgres database

I find it easiest to use visual database connection tools.

If you are unsure what client to use: I've written down my top 5 database clients.

We'll use TablePlus to connect to our newly created Postgres database.

Open the app, and create a new connection. For the type, you can choose "PostgreSQL".

TablePlus new PostgreSQL database connection

As for the connection details, you should use:

  • User: The one you just created, in my case chris.
  • Password: The password you set in my case password.
  • Database: This will be postgres.

The rest of the data should already be set up correctly.

You can then test the connection by clicking the "Test" button at the bottom. It should turn everything green.

Postgres connection green

And that's it you are now connected to your Postgres database.

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Did you find this article valuable?

Support Chris Bongers by becoming a sponsor. Any amount is appreciated!