Page tree
Skip to end of metadata
Go to start of metadata

MacOS 10.7 Lion

  • Install XCode (get it from the app store)
  • Install Mac Ports.

 

  sudo port install postgresql91 postgresql91-server
  sudo mkdir -p /opt/local/var/db/postgresql91/defaultdb
  sudo chown postgres:postgres /opt/local/var/db/postgresql91/defaultdb

Now add /opt/local/var/db/postgresql91 to your path. This is necessary because OS X Lion includes a copy of posgresql and you need to make sure that the postgres tools point to the installed version.

 

Create the user:

createuser -U postgres -s $USER
createdb $USER

Fix the memory settings

http://stackoverflow.com/questions/6861945/postgresql-failing-with-below-error-on-lion-os-x

http://michael-rushanan.blogspot.ch/2012/06/os-x-lion-mysql-postgres-and-dscl.html

 

sudo sysctl -w kern.sysv.shmall=65536
sudo sysctl -w kern.sysv.shmmax=16777216

 
#or maintain it over reboot
#vi /etc/sysctl.conf
#kern.sysv.shmall=65536
#kern.sysv.shmmax=16777216

 

Have postgres run on startup:

sudo launchctl load -w /Library/LaunchDaemons/org.macports.postgresql91-server.plist
 

 

Or start it now:

sudo su postgres -c '/opt/local/lib/postgresql91/bin/postgres -D /opt/local/var/db/postgresql91/defaultdb' &

For development it's useful to disable authentication from localhost. To do so edit the pg_hba.conf file. On 9.3 this file is located at 

/Library/PostgreSQL/9.3/data/pg_hba.conf

And modify "md5" to "trust" for the localhost connections.

local all all trust
host all all 127.0.0.1/32 trust
host all all ::1/128 trust

Older versions

Mac Ports

  • Make sure XCode is installed, you can get it from the NAS of D-BSSE.
  • Install Mac Ports.
  • Update port database as localadmin:

    localadmin$ sudo port -v selfupdate

PostgreSQL

If you are upgrading to a new version (not patch-level), like e.g. 8.2 -> 8.3, you need to perform a dump/restore cycle.

  • If you have installed on older version, uninstall this version first (after dumping all databases you want to keep):

    localadmin$ sudo port -v uninstall postgresql82 postgresql82-server postgresql82-doc
    
  • Install PostgreSQL as user localadmin, this will take a while:

    localadmin$ sudo port -v install postgresql83 postgresql83-server postgresql83-doc
    
  • By default, Mac Portswill create postgresql database directory and initialize the db:

    localadmin$ ls -al /opt/local/var/db/postgresql83/
    total 0
    drwxr-xr-x    3 root      admin     102 Feb 14 20:03 ./
    drwxrwxr-x    5 root      admin     170 Apr  6 18:05 ../
    drwx------   15 postgres  postgres  510 Apr  8 08:29 defaultdb/
    
  • If the db directory is not created, follow the step below:

    localadmin$ sudo mkdir -p /opt/local/var/db/postgresql83/defaultdb
    localadmin$ sudo chown postgres:postgres /opt/local/var/db/postgresql83/defaultdb
    localadmin$ sudo su postgres -c '/opt/local/lib/postgresql83/bin/initdb -D /opt/local/var/db/postgresql83/defaultdb'
    
  • Configure pg_hba.conf for local connections in the trust mode. Per default all local connections are allowed. So no change is needed.
  • Enable PLpgSQL. The following command enables PL/pgSQL for specified database:

    localadmin$ /opt/local/lib/postgresql83/bin/createlang -U postgres plpgsql template1
    
  • To launch PostgreSQLon startup:

    localadmin$ sudo launchctl load -w /Library/LaunchDaemons/org.macports.postgresql83-server.plist
    

Manual start

If you want to start the postgres now and don't restart the system. Do following command as localadmin:

localadmin$ sudo su postgres -c '/opt/local/lib/postgresql83/bin/postgres -D /opt/local/var/db/postgresql83/defaultdb'

PostgreSQL pgAdmin3

pgAdmin III is a database design and management application for use with PostgreSQL. It is available as port pgadmin3 from Mac Ports. So just do

localadmin$ sudo port install pgadmin3

Performing a dump/restore cycle to migrate databases from one version to another

  1. Dump (with the old version of pg_dump, in this example 8.2):

    $ pg_dump  -b -c -C -v -Fc -Z7 -f <db_name>.dump <db_name>
    
  2. Deinstall the old version, install the new version (see above).
  3. Restore using the new version of pg_restore(in this example 8.3):

    $ pg_restore -Cv -d template1 <db_name>.dump
    

1 Comment

  1. Unknown User (tpylak)

    I had also to create 'postgres' user:
    /opt/local/lib/postgresql83/bin/createuser -U YourLogin -s postgres