Getting Started with PostgreSQL on Mac Part 1

This time we will discuss:

  1. Installing postgresql using homebrew
  2. Configuring a cluster
  3. Starting a server
  4. Stopping a server
  5. 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:

CONSOLE
1brew install postgresql
Click to expand and view more

Once installation is done, check just installed postgresql version using this command:

CONSOLE
1psql -V
2# or
3postgres -V
Click to expand and view more

Output will be like:

CONSOLE
1# output
2postgres (PostgreSQL) 12.4
Click to expand and view more

Configure cluster and start server

After successfully installing postgresql, in terminal log there will be this note:

CONSOLE
 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
Click to expand and view more

So, first create your cluster using this command:

CONSOLE
1initdb --locale=C -E UTF-8 /usr/local/var/postgres
Click to expand and view more

When it’s done, at the end of the the terminal log, will say this:

CONSOLE
1Success. You can now start the database server using:
2
3    pg_ctl -D /usr/local/var/postgres -l logfile start
Click to expand and view more

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:

CONSOLE
1pg_ctl -D /usr/local/var/postgres -l logfile start
Click to expand and view more

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:

CONSOLE
1waiting for server to start.... done
2server started
Click to expand and view more

If you want to start the server automatically during login, this command will add postgresql service to launchd.

CONSOLE
1brew services start postgresql
Click to expand and view more

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.

CONSOLE
1# output
2psql: error: could not connect to server: FATAL:  database "macuser" does not exist
Click to expand and view more

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:

CONSOLE
1createdb
Click to expand and view more

Type and enter psql on terminal again and the postgresql shell should be opened:

CONSOLE
1psql (12.4)
2Type "help" for help.
3
4macuser=#
Click to expand and view more

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:

CONSOLE
1psql postgres
Click to expand and view more

or enter the shell as a role

CONSOLE
1psql postgres -U <username>
Click to expand and view more

Stop server

To stop the server, use this pg_ctl command:

CONSOLE
1pg_ctl -D /usr/local/var/postgres stop
Click to expand and view more

Checking the log

If you start the server using -l option, just open the file that is specified there. In this case logfile:

CONSOLE
1less logfile
Click to expand and view more

In this file we could see the url of the server as well:

CONSOLE
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
Click to expand and view more

Start searching

Enter keywords to search articles

↑↓
ESC
⌘K Shortcut