Few days ago my company computer was upgraded. I waved goodbye to ~3,5 years old 5400rpm HDD and said hello to the brand new SSD.
Because replacing hard drive in my case signifies system installation I decided to migrate from (not so fresh..) Snow Leopard to the latest OS X release.
In this post I’ll show you how to install & run PostgreSQL 9.x on the fresh OS X 10.9 Mavericks instance.
If you don’t have fresh installation (but upgraded one and you experienced any issues related to existing postgres installation) you could try uninstall pg and follow steps #0 and #1.
It may work, but doesn’t have to..
I didn’t have opportunity to test it in a such case so regardless of the outcome any feedback will be welcome.
Step #0 – Install homebrew
Homebrew is a missing package manager for OSX that enables you to install 3rd party software (with satisfying their dependencies) using one command.
Installation is pretty easy and comes down to run a single command in the terminal:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)" |
Step #1 – Install PostgreSQL
You can install the latest version of PostgreSQL by typing:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
brew install postgresql |
However, if you want to install a specific version (let’s assume latest 9.2.x instead of 9.3.x) then you should use ‘brew versions’:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cd /usr/local/Library/Formula/ | |
brew versions postgresql | |
# 9.3.1 git checkout 89b2d08 /usr/local/Library/Formula/postgresql.rb | |
# 9.3.0 git checkout ae59e09 /usr/local/Library/Formula/postgresql.rb | |
# 9.2.4 git checkout e3ac215 /usr/local/Library/Formula/postgresql.rb | |
git checkout e3ac215 /usr/local/Library/Formula/postgresql.rb | |
brew install postgres |
Step #2 – Initialize database cluster
You have to create a database cluster. It’s not so complicated as it sounds:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
initdb /usr/local/var/postgres -E utf8 |
This command creates all the necessary directories in which the database data will live. For more information see the initdb manual.
Step #3 – Run postgres
It’s time to run installed postgres instance:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
postgres -D /usr/local/var/postgres |
The path given as an argument must match path used with initdb in Step #2
If you want to start PostgreSQL automatically after log-in run the following command:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents |
It will create symbolic link to /usr/local/opt/postgresql/homebrew.mxcl.postgresql.plist inside ~/Library/LaunchAgents/ directory.
Step #4 – Create a role
Last but not least – you have to create some user which will be able to create databases:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
createuser -d -P postgres |
For more informations visit createuser documentation.
Step #5 – Do the victory dance, install additional tools
That’s all! From now you have full-fledged postgres instance running on your Mac 🙂
Please help me improve this guide and share you suggestions in comments below.
Hi, many thanks for this tutorial, it’s very useful!
Definitely helpful. However, I prefer not to add extraneous user accounts so I skipped step #4. Instead, I downloaded pgAdmin3 and used it to add new connections, databases, and logins – including an “admin” login with appropriate permissions.
The URL in the ruby install for step one seems to be incorrect. Should be:
ruby -e “$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)”
Fixed, thanks!
And what about Postgress.app from Heroku (http://postgresapp.com) ? Waaaay more easy for not so geeeky people 😉
Good point.
In my case I needed control over pg’s version – which seems to be straightforward [*] when using homebrew.
[*] – for those who are enough ‘geeky’ to be familiar with git & homebrew, of course 😉
I was using this app before, but it was adding to much extra to my Mac. It was taking quite an effort to remove everything to clean the system, what would be even more difficult for not so geeky people. And honestly not so geeky people wouldn’t want to install postgres anyway 😉
You could also just use the postgres installer for OSX, choosing the installer for the specific version you need:
http://www.enterprisedb.com/postgresql-934-installers-osx&ls=Crossover&type=Crossover
Actually, the link I included was to a specific installer for version 9.3.4. The page that lists all installers is here
http://www.enterprisedb.com/products-services-training/pgdownload#osx
Thanks Mike!
Hi, thanks for putting this up.
When I run step 2 initdb /usr/local/var/postgres -E utf8, I get the following:
$ initdb /usr/local/var/postgres -E utf8
The files belonging to this database system will be owned by user “ben”.
This user must also own the server process.
The database cluster will be initialized with locale “en_US.UTF-8”.
The default text search configuration will be set to “english”.
Data page checksums are disabled.
initdb: directory “/usr/local/var/postgres” exists but is not empty
If you want to create a new database system, either remove or empty
the directory “/usr/local/var/postgres” or run initdb
with an argument other than “/usr/local/var/postgres”.
Installing homebrew and postgres was no problem.
Further, when I run just “postgres”, I get
$ postgres
postgres does not know where to find the server configuration file.
You must specify the –config-file or -D invocation option or set the PGDATA environment variable.
This is different than when I had the “postmaster.pid” in the /var/postgres folder. There’s very little literature on this on the web.
Any ideas?
Hi, Ben.
It looks like you already have some postgresql cluster on your system.
You have to pass path to the configuration file or cluster directory.
For example (passed cluster directory, as described in Step #3):
However, since homebrew brings some launchctl plists, you don’t need to start postgres manually. It can start automatically with other deamons/system services, see “Step #3 – Run postgres” for more details.
You can also use pg_ctl to check if the postgres is running on the given db cluster and what parameters was passed to it:
I hope this helps.
Hi, I’m running postgres on maverics, installed by admin user – trying to access postgres as any other user (even other administrators) gives the same error as Ben. Specifying the path postgres -D /usr/local/var/postgres (which is also where it is on my system) doesn’t work and gives the error.
postgres cannot access the server configuration file “/usr/local/var/postgres/postgresql.conf”: Permission denied
Do you have any idea of the permissions setup for running a postgres cluster with diff users.
Hi Phil,
First – please forgive me late reply.
Second – what did you mean by “access postgres as any other user”?
If you want to connect with postgres, use psql command instead (it’s a client app).
The command that you has been using (postgres -D ..) is aimed to start a new postgres server instance. Is it your goal?
I’ve also installed launchy gem to be able to start and stop database.
$ gem install launchy
Launchy uses plist files found from Library/LaunchAgents directory to launch services. To start and stop postgres I just need to execute following commands:
$ launchy start postgres
$ launchy stop postgres
Thanks man!