Installing PostgreSQL 9.6 using MacPorts

In another post, I describe how to install PostgreSQL with PL/R. The purpose of this post is to provide a streamlined version without PL/R.

1. Install PostgreSQL using MacPorts..
Of course, I assume you’ve got MacPorts up and running on your system.

1
2
sudo port install postgresql96 +perl +python27
sudo port install postgresql96-server

2. Set up PostgreSQL

I first need to initialize the database cluster and then get the server running. The following comes straight from the on-screen instructions supplied with the MacPorts port postgresql96-server. I wonder if it would work if I used the existing (and hidden) _postgres user. I also wonder if I should use the --encoding=UTF8 --locale=en_US options I used in my earlier installation.

1
2
3
sudo mkdir -p /opt/local/var/db/postgresql96/defaultdb
sudo chown postgres:postgres /opt/local/var/db/postgresql96/defaultdb
sudo su postgres -c '/opt/local/lib/postgresql96/bin/initdb -D /opt/local/var/db/postgresql96/defaultdb'

Sometimes permissions are messed up (and PostgreSQL will complain). To fix these:

1
sudo chmod -R 755 /opt/local/var/db/

Note that MacPorts creates a launch daemon. To load it now and to make sure it launches on system start, do:

1
2
sudo defaults write /Library/LaunchDaemons/org.macports.postgresql96-server.plist Disabled -bool false
sudo launchctl load /Library/LaunchDaemons/org.macports.postgresql96-server.plist

Then do some set-up to get the database going.

1
2
sudo su - postgres
/opt/local/lib/postgresql96/bin/psql -U postgres -d template1

I tend to do my work using a database named “crsp” (originally I just had CRSP data in there) under my own user name. So I set stuff up accordingly. Within psql:

1
2
3
4
5
CREATE USER igow SUPERUSER CREATEDB CREATEROLE PASSWORD 'xxxxx';
CREATE LANGUAGE plperl;
CREATE LANGUAGE plpython2u;
CREATE DATABASE crsp OWNER igow;
\q

4. Figure out how to get data into the database.

Advertisements
Report this ad
Report this ad
This entry was posted in Uncategorized. Bookmark the permalink.

2 Responses to Installing PostgreSQL 9.6 using MacPorts

  1. Alvaro Soto says:

    Hi,
    if you follow your guide step by step you’ll get an error executing

    $ sudo su postgres -c ‘/opt/local/lib/postgresql96/bin/initdb -D /opt/local/var/db/postgresql96/defaultdb’

    shell-init: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
    could not identify current directory: Permission denied
    could not identify current directory: Permission denied
    could not identify current directory: Permission denied
    The program “postgres” is needed by initdb but was not found in the
    same directory as “initdb”.
    Check your installation.

    It can be fixed doing this extra step after:

    $ cd /opt/local/

    Best.

Leave a Reply