I’ll be using Postgresql for my database. It’s a strong open source database. For many years it has solely been a relational database management system. Recently they have added JSON support and support for NO-SQL style databases. We’ll be using it as a relational database, but the development and capabilities of Postgresql make it the best choice at this time.

I’ve had some old databases lying around since Postgres 9.2 and I’ve decided this is the time to clean up my machine and start fresh with the latest version (9.4.4). I installed 9.2 with homebrew so uninstall is a breeze with:

$ homebrew uninstall postgresql

I’ll install the latest with homebrew as well:

$ homebrew install postgresql
To have launchd start postgresql at login:
  ln -sfv /usr/local/opt/postgresql/\*.plist ~/Library/LaunchAgents
Then to load postgresql now:
  launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
Or, if you don't want/need launchctl, you can just run:
  postgres -D /usr/local/var/postgres

Sweet! I’ll wont be using launchctl right now so I’ll start postgres with the last command.

LOG:  skipping missing configuration file "/usr/local/var/postgres/postgresql.auto.conf"
FATAL:  database files are incompatible with server
DETAIL:  The data directory was initialized by PostgreSQL version 9.2, which is not compatible with this version 9.4.4.

Looks like I have some leftover files from 9.2. Some directory was started with 9.2 and I can’t start 9.4.4 from the 9.2 version. Let’s remove this directory and re-initialize the database.

$ rm -rf /usr/local/var/postgres && initdb /usr/local/var/postgres
...
...
Success. You can now start the database server using:

    postgres -D /usr/local/var/postgres
or
    pg_ctl -D /usr/local/var/postgres -l logfile start

Sweet! Let’s try this again

FATAL:  lock file "postmaster.pid" already exists
HINT:  Is another postmaster (PID 67098) running in data directory "/usr/local/var/postgres"?

Hrm… Looks like we killed the postgres and deleted the data directory with stopping the server. Now I have a orphan process running which is blocking my new postgres process. First I need to find which PID it is. (I know it shows up but it’s good to know where it lives.)

$ cat /usr/local/var/postgres/postmaster.pid
67098
/usr/local/var/postgres
1439688737
5432
/tmp
localhost
  5432001   1245184

The first number is the PID so I’ll send the kill command to it. Do not use kill -9! When I killed the PID and started postgres I got the same message so I had a kill a few PIDs. Now I can create a db

$ createdb recipe
$ psql recipe
psql (9.4.4)
Type "help" for help.

recipe=#

Typing psql gets my into the database where you can run SQL statements. Typing \q get you out of the database and back to the command line. That’s all for today!

Comments