1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Installing PostgreSQL 9.4 on OS X 10.11 using MacPorts

Last updated at Posted at 2015-11-16

This article describes how to install PostgreSQL version 9.4 on OS X using MacPorts.

Environment:

  • Hardware: MacBook Pro (Retina, 13-inch, Late 2013)
  • OS: OS X El-Capitan version 10.11.1
  • Package Manager: MacPorts version 2.3.4

You will need to install MacPorts and selfupdate it as needed.

Installation steps:

  1. Install PostgreSQL using MacPorts
  2. Initialize client
  3. Initialize database
  4. Set startup
  5. Start using

Install PostgreSQL using MacPorts

Here I will install the latest version of PostgreSQL available which is version 9.4.5 at the moment. Run the following command to install it using MacPorts.

$ sudo port install postgresql94-server

It will also install any required packages. It may take a few minutes to finish.

At the end, it will show notes about things to do after installation. I recommend you to take note or save that part for review later.

...
--->  Configuring postgresql94-server
--->  Building postgresql94-server
--->  Staging postgresql94-server into destroot
--->  Creating launchd control script
###########################################################
# A startup item has been generated that will aid in
# starting postgresql94-server with launchd. It is disabled
# by default. Execute the following command to start it,
# and to cause it to launch at startup:
#
# sudo port load postgresql94-server
###########################################################
--->  Installing postgresql94-server @9.4.5_0
--->  Activating postgresql94-server @9.4.5_0

To create a database instance, after install do
 sudo mkdir -p /opt/local/var/db/postgresql94/defaultdb
 sudo chown postgres:postgres /opt/local/var/db/postgresql94/defaultdb
 sudo su postgres -c '/opt/local/lib/postgresql94/bin/initdb -D /opt/local/var/db/postgresql94/defaultdb' 

--->  Cleaning postgresql94-server
--->  Updating database of binaries
--->  Scanning binaries for linking errors
--->  No broken files found.

Initialize client

When you install PostgreSQL server package postgresql94-server, the client package will also be installed. Here, we will initialize the client so we can use that.

First, run the following command to check the available option.

$ port select --list postgresql
Available versions for postgresql:
	none (active)
	postgresql94

It shows that we have option to select postgresql94 as client. Then run the following command to set that.

$ sudo port select --set postgresql postgresql94
Selecting 'postgresql94' for 'postgresql' succeeded. 'postgresql94' is now active.

And when you run the port select --list again, you will see something like this.

$ port select --list postgresql
Available versions for postgresql:
	none
	postgresql94 (active)

Latstly, run this command to check the accessibility of the PostgreSQL client tools.

$ which psql
/opt/local/bin/psql

Initialize database

Next, we will initialize the first, default database. I will show you the steps instructed by MacPorts as shown at the end of the installation (see above), but you may also follow the direction from the PostgreSQL as written in First steps - PostgreSQL. Be careful that some of direction mentioned there will not be run the same in the environment explained in this article.

First, we will create the directory to hold the database files.

$ sudo mkdir -p /opt/local/var/db/postgresql94/defaultdb

Then, we will set the ownership of the directory. There is a special user named postgres which will be the owner of the process of PostgreSQL server.

$ sudo chown postgres:postgres /opt/local/var/db/postgresql94/defaultdb

And lastly, we will run the following command to initiate the database.

Before you proceed, be sure that you are not at any directory within your home directory or any private directory. The command will fail if it has no read access to the current directory. At the following snippet, first I change the current directory to /opt/local/ which is the base directory of MacPorts where every user has read access to it.

$ cd /opt/local/
$ sudo su postgres -c '/opt/local/lib/postgresql94/bin/initdb -D /opt/local/var/db/postgresql94/defaultdb' 

The command will run and you will see output something like this.

The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

...

Success. You can now start the database server using:

    /opt/local/lib/postgresql94/bin/postgres -D /opt/local/var/db/postgresql94/defaultdb
or
    /opt/local/lib/postgresql94/bin/pg_ctl -D /opt/local/var/db/postgresql94/defaultdb -l logfile start

You will see the information regarding the database you have been created and how to start it manually. You can start, manually, the database using one of two snippets by running the postgres or pg_ctl command, or by using the autostart mechanism provided with OS X as described in the next step.

Set startup

Next, we will set the the database to start when the system start. As instructed by MacPorts as shown at the end of the installation (see above), run the following command to start the process and enable the autostart through the OS X launchd.

$ sudo port load postgresql94-server

If you don't want to start manually, follow the steps before to start the database.

Start using

Now, we can access the database using the default user postgres by using one of the following commands.

sudo -u postgres psql

or

psql -U postgres

Afterward, you may create additional user, schema, or database as you needed.

By default, no matter which user you use, which database or schema you access, you will not be asked for password when you access locally from your computer, and no remote access is allowed. You can change this behaviour by modifying the file /opt/local/var/db/postgresql94/defaultdb/pg_hba.conf.

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?