I recently installed PostgreSQL 9.3 using MacPorts. I know some of you use HomeBrew but I still have enough things installed using MacPorts, that work and upgrade well, that I'm still in the MacPorts camp. MacPorts installed PostgreSQL OK, but getting the installation initialized and running took a few hours on a Saturday. Here is a shell script to summarize what finally worked for me.
#!/bin/bash | |
PGV=93 | |
PGBIN=/opt/local/lib/postgresql${PGV}/bin | |
PGDEFDB=/opt/local/var/db/postgresql${PGV}/defaultdb | |
PGLOG=/opt/local/var/log/postgresql${PGV}/postgres.log | |
DBUSER=${USER} | |
## I had ALREADY used MacPorts to install the latest PostgreSQL server. | |
# sudo port -v selfupdate | |
# sudo port -v install postgresql${PGV}-server | |
echo initial database setup | |
sudo mkdir -p ${PGDEFDB} | |
sudo chown postgres:postgres ${PGDEFDB} | |
### added cd ${PGBIN} after reading: http://blog.mirotin.net/131/error-initialize-default-postgres-db | |
## if you want SQL-ANSI default encoding: | |
# sudo su postgres -c "cd ${PGBIN} && ./initdb -D ${PGDEFDB}" | |
## if you want UTF8 default encoding: | |
sudo su postgres -c "cd ${PGBIN} && ./initdb -E UTF8 -D ${PGDEFDB}" | |
echo start server | |
## If you want to run the server, log output to console, ^C to stop the server: | |
# sudo su postgres -c "${PGBIN}/postgres -D ${PGDEFDB}" | |
## If you want to run the server and have the log output go to a file: | |
sudo su postgres -c "cd ${PGBIN} && ./pg_ctl -D ${PGDEFDB} -l ${PGLOG} start" | |
echo setup automatic server start | |
sudo launchctl load -w /Library/LaunchDaemons/org.macports.postgresql${PGV}-server.plist | |
echo remember to setup PATH in your .bashrc | |
echo example: "export PATH=${PGBIN}:\$PATH" | |
echo create dbuser to match OSX username | |
${PGBIN}/createuser --superuser ${DBUSER} -U postgres | |
echo to create a new db: ${PGBIN}/createdb dbname | |
exit 0 |
In creating the script above, I started with hints from:
https://coderwall.com/p/xezzaa
I also needed these hints on OS X postgres user creation:
http://www.working-software.com/node/30
sudo dscl . change /Users/postgres NFSHomeDirectory /opt/local/var/db/postgresql82 /opt/local/var/db/postgresql93
This did not work:
sudo dscl . change /Users/postgres RealName "\n PostgreSQL-82 Server" 'PostgreSQL-93 Server'
<main> attribute status: eDSAttributeNotFound
<dscl_cmd> DS Error: -14134 (eDSAttributeNotFound)
But this did work:
sudo dscl . -create /Users/postgres RealName "PostgreSQL Administrator"
See: http://gilmation.com/articles/compile-and-configure-postgresql-on-mac-os-x/