This time we will discuss:
- Installing
postgresqlusinghomebrew - Configuring a cluster
- Starting a server
- Stopping a server
- Viewing redirected log
Install postgreSQL using homebrew
There are multiple ways to install postgresql on your mac. Let’s install it through command line for this post.
You need to have homebrew installed before continuing. Check here Homebrew
on how to install homebrew.
Open terminal and run this:
1brew install postgresql
Once installation is done, check just installed postgresql version using this command:
1psql -V
2# or
3postgres -V
Output will be like:
1# output
2postgres (PostgreSQL) 12.4
Configure cluster and start server
After successfully installing postgresql, in terminal log there will be this note:
1==> postgresql
2To migrate existing data from a previous major version of PostgreSQL run:
3 brew postgresql-upgrade-database
4
5This formula has created a default database cluster with:
6 initdb --locale=C -E UTF-8 /usr/local/var/postgres
7For more details, read:
8 https://www.postgresql.org/docs/12/app-initdb.html
9
10To have launchd start postgresql now and restart at login:
11 brew services start postgresql
12Or, if you don't want/need a background service you can just run:
13 pg_ctl -D /usr/local/var/postgres start
So, first create your cluster using this command:
1initdb --locale=C -E UTF-8 /usr/local/var/postgres
When it’s done, at the end of the the terminal log, will say this:
1Success. You can now start the database server using:
2
3 pg_ctl -D /usr/local/var/postgres -l logfile start
Start your server using pg_ctl,
it is a utility to initialize, start, stop, or control a PostgreSQL server.
Full documentation Postgresql . Let’s start the server by running this command:
1pg_ctl -D /usr/local/var/postgres -l logfile start
This will start a database cluster server we specify in -D option (/usr/local/var/postgres) and redirect the log to a file using -l option with file name logfile in the current directory.
The output will be like:
1waiting for server to start.... done
2server started
If you want to start the server automatically during login, this command will add postgresql service to launchd.
1brew services start postgresql
Let’s try to open the postgresql shell, try to type and run psql command on terminal.
If you get this output error, it means the database is not created yet.
1# output
2psql: error: could not connect to server: FATAL: database "macuser" does not exist
In Mac, after installation using brew, you a default user with the mac username is created but the database is not. You need to run createdb in the terminal. then you can enter the shell as your mac user name (superuser) with only entering psql.
Other terminal utilities for postgreSQL is documented PostgreSQL . This often happen on Mac. Just create a database by running this command on terminal:
1createdb
Type and enter psql on terminal again and the postgresql shell should be opened:
1psql (12.4)
2Type "help" for help.
3
4macuser=#
The # sign tells that you are logged in as a role with superuser capabilities.
Type \q to exit the shell.
You can enter the postgres shell as the default superuser role with this command:
1psql postgres
or enter the shell as a role
1psql postgres -U <username>
Stop server
To stop the server, use this pg_ctl command:
1pg_ctl -D /usr/local/var/postgres stop
Checking the log
If you start the server using -l option, just open the file that is specified there. In this case logfile:
1less logfile
In this file we could see the url of the server as well:
12020-08-31 02:12:26.089 BST [43338] LOG: starting PostgreSQL 12.4 on x86_64-apple-darwin19.5.0, compiled by Apple clang version 11.0.3 (clang-1103.0.32.62), 64-bit
22020-08-31 02:12:26.090 BST [43338] LOG: listening on IPv6 address "::1", port 5432
32020-08-31 02:12:26.090 BST [43338] LOG: listening on IPv4 address "127.0.0.1", port 5432
42020-08-31 02:12:26.092 BST [43338] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"
52020-08-31 02:12:26.113 BST [43339] LOG: database system was shut down at 2020-08-31 02:12:17 BST
62020-08-31 02:12:26.119 BST [43338] LOG: database system is ready to accept connections
