Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install PostgreSQL on OSX Yosemite using MacPorts

I want to install PostgreSQL for a node project that I'm developing in OSX Yosemite. I use MacPorts and so tried the method described here: https://github.com/codeforamerica/ohana-api/wiki/Installing-PostgreSQL-with-MacPorts-on-OS-X

...but I get an error during step 2:

$ sudo gem install pg -- --with-pg-config=/opt/local/lib/postgresql93/bin/pg_config > ruby_error
ERROR:  Error installing pg:
    ERROR: Failed to build gem native extension.

    /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby extconf.rb --with-pg-config=/opt/local/lib/postgresql93/bin/pg_config
Using config values from /opt/local/lib/postgresql93/bin/pg_config
checking for libpq-fe.h... yes
checking for libpq/libpq-fs.h... yes
checking for pg_config_manual.h... yes
checking for PQconnectdb() in -lpq... no
checking for PQconnectdb() in -llibpq... no
checking for PQconnectdb() in -lms/libpq... no
Can't find the PostgreSQL client library (libpq)
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

...thinking that I may not need to install the pg gem since I want to work with Node and not Ruby, I moved on to the next steps. But there I run into an error during step 3.3:

$ sudo su postgres -c '/opt/local/lib/postgresql93/bin/initdb -D /opt/local/var/db/postgresql93/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.

...checking my /opt/local/lib/postgresql93/bin/ directory, I see both initdb and postgres. I see those lines saying Permission denied and am wondering what that's about.

Not sure how to progress. Thinking of using Postgres.app if it really is easier but not sure whether it would be better to install using MacPorts since I install most other things using MacPorts. Tips about any of my problems are appreciated!

like image 797
Miguel Valencia Avatar asked Feb 09 '15 08:02

Miguel Valencia


People also ask

What is the best way to install PostgreSQL on Mac?

Step by step guide to set up a local PostgreSQL on Mac using DBngin: Download DBngin from it's download page and install on your Mac just like any other Mac apps. Open DBngin and click on + new server button. Choose PostgreSQL from the driver list, choose your PostgreSQL version, and name you server.

Where is psql installed on Mac?

The actual database files will be under /usr/local/var/postgres after you create the database. So, just create a database and then see what's new or changed under /usr/local/var/postgres .

How do I start PostgreSQL on Mac terminal?

`psql` on Terminal To get to the PostgreSQL terminal, open your terminal, start the database services ( brew services start postgresql) , then run psql . Thank you!


1 Answers

The permissions/ownership on the directories between / and defaultdb likely need to be fixed. I think PostgreSQL can be sensitive to ownership of these, though it seems that in your case PostgreSQL simply doesn't have access to those. This is what I have for each directory.

$ ls -hlt /opt/local/var/db/
total 0
drwxr-xr-x  7 root  admin   238B Jan 23 16:54 texmf
drwxr-xr-x  3 root  admin   102B Dec 25 07:37 postgresql94

You could fix permissions by doing sudo chmod a+rx /opt/local/var/db/ as needed.

For the defaultdb directory itself, you should follow the instructions that you link to, which seem to have the same as I have:

sudo chown postgres:postgres /opt/local/var/db/postgresql93/defaultdb

Below are instructions adapted from my blog (though I recommend using PostgreSQL 9.4, which I now do). I've been running PostgreSQL using MacPorts since 9.1 without major problems.

1. Install PostgreSQL using MacPorts.

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

sudo port install postgresql93 +perl +python27
sudo port install postgresql93-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 postgresql93-server.

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

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

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

I then use psql for some set-up to get my database going.

sudo su - postgres
/opt/local/lib/postgresql93/bin/psql -U postgres -d template1

If you get to here, then you have PostgreSQL running on your system.

like image 56
Ian Gow Avatar answered Sep 28 '22 07:09

Ian Gow