Install PostgreSQL on MacOS

Author
By Darío Rivera
Posted On in PostgreSQL

To install this database engine, I recommend having previously installed homebrew. If you have already installed PostgreSQL and you want to know some console commands, visit our article Connection by terminal to PostgreSQL. Having said that, let's get started with the installation.

Installation

Installation is just a matter of executing the following command with homebrew.

brew install PostgreSQL

To verify the installed version, we can use the following command:

thor: dario$ postgres -V
postgres (PostgreSQL) 12.1

Creating the database cluster

The installation of PostgreSQL should have created a database cluster. To verify this, simply check that the following directory is created.

ls /usr/local/var/postgres/

If it is not, you must create a new PostgreSQL cluster with the following command:

initdb /usr/local/var/postgres

A cluster is nothing more than a collection of databases that are managed by a server instance.

Starting the database service

Taking into account the cluster created in the previous step, starting or stopping the database service is just a matter of executing the following commands as appropriate.

initdb /usr/local/var/postgrespg_ctl -D /usr/local/var/postgres start
pg_ctl -D /usr/local/var/postgres stop

If the PostgreSQL cluster was created automatically during the installation, you probably won't have to start the service manually. The output of the service startup command should be similar to the following if everything has gone well.

waiting for server to start....2020-01-05 11:20:06.259 -05 [30885] LOG:  starting PostgreSQL 12.1 on x86_64-apple-darwin18.7.0, compiled by Apple clang version 11.0.0 (clang-1100.0.33.12), 64-bit
2020-01-05 11:20:06.264 -05 [30885] LOG:  listening on IPv6 address "::1", port 5432
2020-01-05 11:20:06.264 -05 [30885] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2020-01-05 11:20:06.267 -05 [30885] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2020-01-05 11:20:06.297 -05 [30886] LOG:  database system was shut down at 2020-01-05 10:55:01 -05
2020-01-05 11:20:06.305 -05 [30885] LOG:  database system is ready to accept connections
 done
server started

Among other things, this output shows us that the server is available to accept connections on port 5432. Once the service is started, it is sufficient to use the simplest connection method specifying only the database to which we will connect. To see more authentication options, we invite you to check our post Connection by terminal to PostgreSQL.

psql postgres

The database postgres should have been created during the installation. To obtain a list of the databases in the cluster, you can execute the command psql -l.

If the service is not set to start up with the operating system, you can execute the following command.

brew services start postgresql

Acerca de Darío Rivera

Author

Application Architect at Elentra Corp . Quality developer and passionate learner with 10+ years of experience in web technologies. Creator of EasyHttp , an standard way to consume HTTP Clients.

LinkedIn Twitter Instagram

Sólo aquellos que han alcanzado el éxito saben que siempre estuvo a un paso del momento en que pensaron renunciar.