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
Homebrew
is installed using below commandbrew -v
If its already installed,
Homebrew
version should be displayed. -
Next, update
Homebrew
to the latest version using below commandbrew update
-
Now install
PostGres
SQL usinghomebrew
commandbrew 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. -
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
. -
Ways to Stop
PostGresql
To use
postgresql
the service should be running. After we are done usingpostgresql
, its good to stoppostgresql
service, as it consumes a lot of resources.To stop
postgresql
use 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
postgresql
service using command,brew services start postgresql
-
Next in terminal, type in below command,
psql postgres
This 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
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 aspassword
. Also we provided the newly created user with roleCREATEDDB.
-
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
.
-
Go to
https://www.pgadmin.org/
and download thePgAdmin
installer and install in your system. -
After
Pgadmin
is installed, openPgAdmin
application , right click onServer
option 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
postgresql
service is started and when we try logging intopostgresql
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
-
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