How to install PostgreSQL on Mac using Homebrew

How to Mac

apple postgresql homebrew brew mac

In this tutorial we will learn to install PostgreSQL database on Mac using Homebrew.

Prerequisite

It is assumed that you have Homebrew installed on your Mac.

If you don't have Homebrew installed on your Mac then open Terminal and run the following command.

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

You can visit Homebrew official website https://brew.sh to learn more about it.

Once you have Homebrew (a.k.a. brew) installed on your machine you can run the following command in the Terminal to check the version.

$ brew -v

To update run the following command.

$ brew update

Alright, time to install PostgreSQL on Mac.

Install PostgreSQL using Homebrew

In Terminal run the following command to install PostgreSQL on Mac using Homebrew.

$ brew install postgres

We can check the version of PostgreSQL using the psql command.

$ psql --version
psql (PostgreSQL) 11.5

Start PostgreSQL

To start PostgreSQL run the following command in the Terminal.

$ brew services start postgres

We will get a similar output shown below.

==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)

Stop PostgreSQL

To stop PostgreSQL run the following command in the Terminal.

$ brew services stop postgres

We will get a similar output.

Stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)

Restart PostgreSQL

To restart PostgreSQL run the following command in the Terminal.

$ brew services restart postgres

We will get a similar output as shown below.

==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)

Login to PostgreSQL database

By default we will get a database by the name postgres. So, to connect to it we will run the following command.

$ psql postgres

We will see the following output.

$ psql postgres
psql (11.5)
Type "help" for help.

postgres=#

List all the users

To list all the users we use the \du command.

postgres=# \du
                                     List of roles
  Role name   |                         Attributes                         | Member of 
--------------+------------------------------------------------------------+-----------
 yusufshakeel | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

List all the databases

To list all the databases run the \l command.

postgres=# \l
                                   List of databases
   Name    |    Owner     | Encoding | Collate | Ctype |       Access privileges       
-----------+--------------+----------+---------+-------+-------------------------------
 postgres  | yusufshakeel | UTF8     | C       | C     | 
 template0 | yusufshakeel | UTF8     | C       | C     | =c/yusufshakeel              +
           |              |          |         |       | yusufshakeel=CTc/yusufshakeel
 template1 | yusufshakeel | UTF8     | C       | C     | =c/yusufshakeel              +
           |              |          |         |       | yusufshakeel=CTc/yusufshakeel
(3 rows)

Create a database

To create a database run the following command. In the given example mydb is the name of the database.

postgres=# CREATE DATABASE mydb;

Connect to a database

To connect to a database use the \c command.

postgres=# \c mydb;
You are now connected to database "mydb" as user "yusufshakeel".

List all the tables inside a database

To list all the tables inside a database we run the \d command.

Note! If there is no table then we will get a prompt stating no relations found.

mydb=# \d
Did not find any relations.

Let us go ahead and create a simple users table inside the mydb database and try the above command again.

Create table

In the following example we are creating a simple users table.

mydb=# CREATE TABLE users (
  id BIGSERIAL PRIMARY KEY,
  firstName VARCHAR(200) NOT NULL,
  middleName VARCHAR(200) DEFAULT NULL,
  lastName VARCHAR(200) DEFAULT NULL
);

Now if we list the tables using the \d command we will get the table.

mydb=# \d
                List of relations
 Schema |     Name     |   Type   |    Owner     
--------+--------------+----------+--------------
 public | users        | table    | yusufshakeel
 public | users_id_seq | sequence | yusufshakeel
(2 rows)

How to exit from psql?

To exit or quit from psql type the \q command.

postgres=# \q

Alright, this brings us to the end of this tutorial. Hope you found it useful. Please share this tutorial if it was helpful. See you in the next tutorial. Have fun developing :-)