How to setup Postgres Database in Mac

Install and Setup Postgres in Mac

In this article we are going to setup PosGresql in Mac.

Setting up PostGresql in Mac is very easy if we make use of Homebrew.

  1. Open terminal and check if Homebrew is installed using below command

    brew -v
    

    If its already installed, Homebrew version should be displayed.

  2. Next, update Homebrew to the latest version using below command

    brew update
    
  3. Now install PostGres SQL using homebrew command

    brew install postgresql
    

    This will fetch the latest postgres and install it in our system.

    Installation will take some time, once completed, we can start the postgresql service.

  4. Now lets start PostGresql

    If postgresql is installed correctly, we can start service using below command,

    brew services start postgresql
    

    If service is successfully started we should get a message saying Successfully started postgresql.

  5. Ways to Stop PostGresql

    To use postgresql the service should be running. After we are done using postgresql, its good to stop postgresql service, as it consumes a lot of resources.

    To stop postgresql use below command

    brew services stop postgresql
    

PostGresql Configuration

Now that we have installed postgresql, we can start configuring the database. The steps to do the same are as shown below,

  1. Start postgresql service using command,

    brew services start postgresql
    
  2. Next in terminal, type in below command,

    psql postgres
    

    This should log us into postgresql. Here we can input SQL commands

  3. We need to create a new user and setup a role before moving ahead. Type in below command in postgresql terminal to create new user and provide role.

    CREATE ROLE newUser WITH LOGIN PASSWORD ‘password’;
    ALTER ROLE newUser CREATEDB;
    

    The above commands will create a user called newUser with login password as password. Also we provided the newly created user with role CREATEDDB.

  4. We can test this user by using below command

    \q
    psql postgres -U newuser
    

    Here we first quit the SQL session using \q and login to new SQL session using the second command.

Install PgAdmin

Instead of using the terminal to use postgresql we can use a rich GUI application like PgAdmin.

  1. Go to https://www.pgadmin.org/ and download the PgAdmin installer and install in your system.

  2. After Pgadmin is installed, open PgAdmin application , right click on Server option and create new server. Add below configuration to login to postgresql.

    host – “localhost”
    user – “newuser”
    password – “password”
    maintenance database – “postgres”
    
  3. A SQL sheet should open. Here we can type in our SQL commands and start using Postgresql.

Issues

Some of the issues that we might face when setting up PostgreSQL are as mentioned below,

  1. After postgresql service is started and when we try logging into postgresql using below command,

    psql postgres
    

    we might get below error

    psql: error: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

To resolve this we need delete node modules. Use below command to do the same

sudo chown -R ${LOGNAME}:staff /usr/local/lib/node_modules

then restart postgresql service using below command,

brew services restart postgres

This should fix the issue. Now we can log into postgresql using command


psql postgres -U newuser
  1. Another error we might stumble onto is as shown below,

    Bootstrap failed: 5: Input/output error Try re-running the command as root for richer errors. Error: Failure while executing; /bin/launchctl bootstrap gui/501 /Users/chiragsp/Library/LaunchAgents/homebrew.mxcl.postgresql.plist exited with 5.

    We can get logs of postgres to see what the error is using command,

    tail /usr/local/var/log/postgres.lo
    

    In logs we can see the error as shown below

    User-MacBook-Air:~ user$ tail /usr/local/var/log/postgres.log 2022-07-06 06:50:15.611 IST [9078] FATAL: database files are incompatible with server 2022-07-06 06:50:15.611 IST [9078] DETAIL: The data directory was initialized by PostgreSQL version 10, which is not compatible with this version 13.4.

    From logs we can understand that we need to update postgresql. To update we can use below commands,

    brew services stop postgresql
    brew postgresql-upgrade-database
    brew services start postgresql
    

    This would fix the issue.

    So that’s how we go about setting up postgresql in Mac. Checkout more articles on development here