Open In App

How to Install PostgreSQL on a Mac with Homebrew

Last Updated : 04 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Homebrew is a popular manager that is used for the installation of different types of software in the Mac as well as Linux operating systems. we can use Homebrew for installing other software instead of running specific commands. This improves the security as compared to not using homebrew for the installation of applications.

This is a step-by-step article where we are going to understand, how we can install PostgreSQL on a Mac operating system using homebrew.

How to Install PostgreSQL on a Mac with Homebrew

PostgreSQL is a popular relational database management system (DBMS) which is also known as Postgres, it is open-source and uses the compliance of SQL for its internal working.

It is an open-source software which is developed by Max Howell and it uses GitHub for giving the packages to the users. before using the homebrew for installing the PostgreSQL we will have to install homebrew on the Mac, following are the steps for installing homebrew:

Step 1: Install Homebrew

Open the terminal and type the following command to install the homebrew from a package source. This will show us the different components of homebrew which will be installed.

/bin/bash -c "$(curl -fsSL
https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Output:

02_installation-complete
homebrew installation.

Once homebrew is installed, we can run the homebrew and install PostgreSQL but if you already had homebrew installed before then you can follow step 2 to update it.

Step 2: Update Homebrew

It is important to update the homebrew because from time to time we get updates of homebrew for latest packages and features so before installing any software we must update the homebrew to the latest version.

brew update

Output:

03_brew-update
brew update.

Once the homebrew is successfully installed, there are some important steps that we need to follow for the successful installation of PostgreSQL if we are using homebrew, these steps are as follows:

1. Download the PostgreSQL installer for Mac.

2. Installing PostgreSQL.

3. Verify the installation.

Let's understand each of these steps one by one.

Step 3: Install Postgres

For downloading and installing the PostgreSQL, we will have to run the following command in the terminal as you can see in the image as well

brew install postgresql

Output:

01_brew-install-postgresql
brew install postgresql

Once the PostgreSQL is installed, we can check its installation by running some commands, it takes some time to successfully install PostgreSQL in the mac using homebrew so be patient. Once it is installed, it will show you the next steps and commands for starting the service, as visible in the image below:

02_start-service-command
brew services start postgresql

Once we run this command we can start the PostgreSQL, it requires us to create a role with user and password along with it so that we can establish connection to the database.

Step 4: Start the Postgres service

In this step, we will create a user and also create a database which will be assigned to that particular user to test whether PostgreSQL is successfully installed with homebrew or not. following are the steps for this:

Step 1: Start Postgresql:

First step is to start the PostgreSQL service, for this we can simply type the psql postgres command and it will start the PostgreSQL service for us. Once the service is started we will see that profile name disappears and instead postgres appears in the terminal.

Output:

03_open-postgres
Start Postgresql

Step 2: Create Role With Password:

The next step is to create a role with password, for this we will have to follow the following syntax:

CREATE ROLE role_name WITH LOGIN PASSWORD password_for_user;

As you can see in the image below, we have followed the same above syntax to create a user named app_user with password as app_password.

Output:

04_create-role-and-password
Create Role With Password.

Step 3: Alter Role for User:

Next step is to alter the role for the user that we have created, role altering means that we are assigning a different role to the user so that the user can perform necessary tasks with admin privileges.

As you can see in the image below, we are assigning the user with the role of CREATEDB which means that the user can create database.

Output:

05_check-if-user-is-created
Alter Role for User.

Step 4: Create Database:

In this next step we will create a database, for creating a database we will first have to login with the user's details that we have created. Simply type psql postgres -U username to login as the user.

Then we will create database with following syntax, as you can see in the image below.

CREATE DATABASE database_name;

Output:

06_create-database
Create Database.

After creating the database we can run the /l command which will list the databases that we have created, as you can see in the first option we have the app_database that we just created.

This is how we can install PostgreSQL on a mac using homebrew, by following these steps we can easily install the PostgreSQL in any mac system.

Troubleshooting

  • Sometimes we may have to perform some troubleshooting of the postgresql because there may be some errors in it.
  • The troubleshooting of postgresql involves solving different issues which can be related to performance or the connection of database as well.
  • Whenever we try to troubleshoot the problem of PostgreSQL, we should look at the logs of the PostgreSQL first.
  • PostgreSQL logs are a built in feature of the software which allows us to look at the detailed information about the errors and any issues if they come and interrupt the process.
  • Troubleshooting these issues are important because it can also result in data corruption as well as crashes, if these issues are not managed properly.
  • For troubleshooting this we can take a look at the logs and the system environment to analyze the issues.

Conclusion

Homebrew can be used for installing postgresql in the mac operating system, for this we will have to first install homebrew in the system and then use the brew install PostgreSQL command to install PostgreSQL in the system. By following each of the steps we can install and manage the database in the PostgreSQL.


Next Article
Article Tags :

Similar Reads