Three Battle Tested Ways to Install PostgreSQL
Having run over 100 students through my Ruby on Rails courses, I've come up with some straight forward ways to install PostgreSQL without too much pain. Here is how I do it:
PostgreSQL is a relational database manager that keeps getting more and more popular within the web development community. It has taken over from MySQL as the preferred tool for production quality, scalable databases. The rise in popularity is likely due to the backlash from Oracle purchasing and messing with MySQL, Heroku choosing Postgres as the preferred database in production, and Postgres' faster pace of development of new features like Arrays and HStore.
How to Install Postgres on Mac OS X
There are a lot of confusing options for installing PostgreSQL. In fact, your Mac most likely came with a system version of Postgres. The problem is that version is already likely out-of-date, and may even be insecure. It is better to keep all programs you use for development in user space instead of installed system-wide if possible, so that you don't mess with what Apple configured the system to expect.
I prefer to use the homebrew package manager because it makes it easier to keep the software up-to-date. Brew also installs all commands needed to manage Postgres to standard locations in your path, which makes it easier to use on the command line, and easier for libraries like Ruby Gems and npm packages to interact with.
This lesson below will teach you how to install PostgreSQL, the easy way, on a Mac. This tutorial is designed for Max OS 10.9 - Mavericks. If you don't have Mavericks, upgrade your mac first.
Open up the Terminal
You'll need to do many of these steps in the command line.
Copy the install command from the Install Homebrew section, near the bottom of the page on
Don't re-install homebrew if you already have it. If you already have it, make sure to do a
brew update to ensure you have the latest package info on your system. If you have MacPorts or Fink installed, this tutorial won't work for you. I recommend switching to Homebrew anyway as it is kept up to date more often. If you have homebrew installed via RVM (it happens), your paths will begin with
/home/username/.rvm/usr/local instead of just
/usr/local - not to worry.
Start homebrew installation
Paste the install homebrew command into the Terminal. It should look somewhat like the picture above.
Install the Command Line Tools
You may already have these if you've been developing for a while. If you see the dialog above, let them install.
Wait for the command line tools to download
This step can take a while, hopefully you are on a good connection.
Enter your password
Homebrew needs your user password to set itself up. Enter it in the terminal. Be careful typing, because you won't see your typing echoed back to you. It may seem like it's not working, but if you enter in your password, and then press return, it should work.
You should see "Installation successful!"
Run brew doctor
You should see "Your system is ready to brew". If not, there should be instructions on how to fix each issue it finds.
brew install postgres
use the command
brew install postgres to get PostgreSQL
Make sure to read the Caveats
It's a lot of text scrolling by and the temptation is to skim over it. ALWAYS read the Caveats section when installing with Homebrew. It contains critical info. I'll go over what you need.
Create the LaunchAgents Directory and Link the plist
mkdir -p ~/Library/LaunchAgents ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
These two commands create a LaunchAgents directory (if it didn't exist already), and then make a symbolic link to the postgres configuration (property list) file. This is how you get PostgreSQL to start up automatically with your Mac.
Load the plist
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
from the homebrew caveats section load the plist into the launch control service so that postgresql will automatically reload if needed.
Load the plist
paste in the launchctl load command
Edit the system paths
Because Mac OS X comes with a system PostgreSQL, we want to make sure to load up our homebrew PostgreSQL first.
Use the command
sudo nano /etc/paths
Copy (or retype)
/usr/local/bin from the bottom of the file and put it on the top line.
Finished editing /etc/paths
Press Ctrl-X to save the file and answer yes.
Write to /etc/paths
Save the file in its existing location (/etc/paths)
Confirm the changes
Use the unix
cat command to confirm that your changes were saved
I know, this is Unix and not windows. But this is seriously the easiest way to do this. Don't skip this step.
After rebooting, your
which psql command should return
Create a default db based on your username
which psql command should return
Make sure to use backticks, they are below the tilde (squiggly line) character underneath the escape key on a US keyboard. This just makes it easier to use the psql command.
Try out the psql command
Great! you can get into psql without any errors
Install the pg gem
gem install pg and you should be good to go.
How to Install Postgres on Windows
Windows is thankfully very straightforward: use the installer from Postgresql.org
You can accept all of the default options. Then, you should be able to
gem install pg if you are using Ruby. If you are using JRuby, you may also want to install the optional JDBC driver for Postgres.
How to Install Postgres on Ubuntu Linux
Ubuntu is not exactly straightforward, but at least it's well-documented. Choose the Basic Setup. Some other things that have helped students are mentioned in the Mac OS X section above. Basically, create an empty database based on your username so that it's easier to get into using the psql command:
For a development machine, it is also easier to set up your user with super-admin privileges.
sudo -u postgres createuser --superuser $USER