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.
-
Open terminal and check if
Homebrewis installed using below commandbrew -vIf its already installed,
Homebrewversion should be displayed. -
Next, update
Homebrewto the latest version using below commandbrew update -
Now install
PostGresSQL usinghomebrewcommandbrew install postgresqlThis will fetch the latest
postgresand install it in our system.Installation will take some time, once completed, we can start the
postgresqlservice. -
Now lets start
PostGresqlIf
postgresqlis installed correctly, we can start service using below command,brew services start postgresqlIf service is successfully started we should get a message saying
Successfully started postgresql. -
Ways to Stop
PostGresqlTo use
postgresqlthe service should be running. After we are done usingpostgresql, its good to stoppostgresqlservice, as it consumes a lot of resources.To stop
postgresqluse below commandbrew 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,
-
Start
postgresqlservice using command,brew services start postgresql -
Next in terminal, type in below command,
psql postgresThis should log us into
postgresql. Here we can input SQL commands -
We need to create a new user and setup a role before moving ahead. Type in below command in
postgresqlterminal 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
newUserwith login password aspassword. Also we provided the newly created user with roleCREATEDDB. -
We can test this user by using below command
\q psql postgres -U newuserHere we first quit the SQL session using
\qand 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.
-
Go to
https://www.pgadmin.org/and download thePgAdmininstaller and install in your system. -
After
Pgadminis installed, openPgAdminapplication , right click onServeroption and create new server. Add below configuration to login topostgresql.host – “localhost” user – “newuser” password – “password” maintenance database – “postgres” -
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,
-
After
postgresqlservice is started and when we try logging intopostgresqlusing below command,psql postgreswe 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
-
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
postgresto see what the error is using command,tail /usr/local/var/log/postgres.loIn 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 postgresqlThis would fix the issue.
So that’s how we go about setting up
postgresqlin Mac. Checkout more articles on development here