How to Install and Start PostgreSQL Server on Mac

Bilal Shahid Feb 15, 2024
  1. Introduction to PostgreSQL Server
  2. Install PostgreSQL Server on Mac
  3. Start PostgreSQL on Your Mac
  4. Test PostgreSQL Installation
How to Install and Start PostgreSQL Server on Mac

The PostgreSQL server is a very powerful and popular one. However, it is different from installing on a Mac compared to a Windows device.

This article walks mac users through successfully installing and starting the PostgreSQL server on their system.

Introduction to PostgreSQL Server

PostgreSQL is an open-source object-relationship database. The server allows you to create, edit, and manipulate databases to your liking.

Its high popularity, resilience, correctness, and integrity are some of the many reasons that contribute to its high demand.

PostgreSQL doesn’t require a companion server as it is made to be completely self-sufficient and uses a language called psql, which is an extension of the original SQL language.

Install PostgreSQL Server on Mac

The first step is to download a PostgreSQL installer for your mac. It is often best to pick the latest version as long as it supports your system. After this, follow the instructions given below:

  1. Double-click the installer file and launch the setup wizard.

    install and start postgresql server on mac - image one

  2. Select the directory where you would like to install PostgreSQL.

  3. Click all of the components that you prefer. Stack Builder is often unincluded, and it is best to uncheck it.

  4. Specify the directory to store data. It is best to pick the default selection.

  5. The installer will now prompt you to enter a password. Here, you must select a password that PostgreSQL will prompt you to enter every time you launch the app.

  6. It will then ask for a port number. It is best to opt for the default port number here as well.

  7. When asked for the locale, select the ["Default Locale"].

  8. After reviewing the installation information, click Next to begin the installation.

  9. After a few minutes, when installation is complete, click Finish, and it will install PostgreSQL.

    install and start postgresql server on mac - image two

When it comes to installation, using Homebrew is also a good option. You can install it by pasting the following code in your OS terminal:

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

You can run brew info postgres to learn more about it after it has been installed.

Start PostgreSQL on Your Mac

Starting PostgreSQL using Homebrew is often more beneficial, depending on your requirements. In most cases, it can be launched automatically. However, you might have trouble getting it done since it is slightly different.

Start PostgreSQL Manually:

pg_ctl -D /usr/local/var/postgres start

Stop PostgreSQL Manually:

pg_ctl -D /usr/local/var/postgres stop

If you have a specific version, it is better to use the following:

brew services start postgresql@10

Most users prefer to get it to start automatically. If this is the case, type in the following command and restart at login.

brew services start postgresql

However, you might experience some issues. To check if you need to check your installation method, try using the following:

pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

OR

pg_ctl -D /usr/local/var/postgres status

Another alternative is lunchy gem, a wrapper for launchctl. To install it, use the following code:

brew install postgresql
initdb /usr/local/var/postgres -E utf8
gem install lunchy

Then to start PostgreSQL, use lunchy start postgres and stop using lunchy stop postgres.

Eradicate Errors (If Any) While Starting PostgreSQL on Mac

If there aren’t any error messages, recheck the syntax you used to start it or reboot your system. If you do get errors on server.log or if it won’t start even if the error messages did not appear, try the initdb command as follows; it tends to solve most problems:

initdb /usr/local/var/postgres -E utf8

If this doesn’t work, try running the command given below, followed by the code above:

rm -rf /usr/local/var/postgres

This, however, removes the existing directory, so if that isn’t what your goal is, this is a good alternative:

mv /usr/local/var/postgres /usr/local/var/postgres.backup

It will move it to a backup directory so you can always retrieve it; then, you can type in the initdb command keeping the following things in mind.

  • Make sure all your TCP local host connections are enabled in pg_hba.conf.

  • In postgresql.conf, check the listen_addresses and port using egrep 'listen|port' /usr/local/var/postgres/postgresql.conf.

    If you change the port, you will have to restart your system for it to be processed.

  • If you used Homebrew, Fink, MacPorts, or EnterpriseDB for installation, use the code corresponding to the installer in use to figure out which package manager installed it.

    brew && brew list|grep postgres
    fink && fink list|grep postgres
    port && port installed|grep postgres
    

    You can substitute the first word with the installer you used if none of the above complies with you. Using the wrong one will display an error message, making it easy to tell.

Test PostgreSQL Installation

Now that it is installed and running, it is best to load a sample database onto it to ensure everything is working correctly. You may do so if you prefer to create a new database from scratch using PostgreSQL.

Here is an example code:

CREATE table new_table(
    name varchar(30),
    age int,
    email varchar(250),
    address varchar(300)
);

If it runs successfully, add some data to it using the INSERT command. You can do it using the following code:

INSERT INTO new_table (name, age, email, address)
VALUES ('aa bb', 30, 'abc@gmail.com', '123, street x');

You may also use other features for testing, but if these two are working, it is highly likely that your PostgreSQL server has been installed correctly and is working perfectly fine on your mac machine.

We hope you have managed to troubleshoot the issue and now know how to start it on mac with ease.

Author: Bilal Shahid
Bilal Shahid avatar Bilal Shahid avatar

Hello, I am Bilal, a research enthusiast who tends to break and make code from scratch. I dwell deep into the latest issues faced by the developer community and provide answers and different solutions. Apart from that, I am just another normal developer with a laptop, a mug of coffee, some biscuits and a thick spectacle!

GitHub

Related Article - PostgreSQL Installation