The easiest way, I have found so far, to install PostgreSQL database on Mac OS X is to use the postgres app. Here are the steps:
Log on to http://postgresapp.com/.
Download the latest version.
Unarchive the downloaded zip file (usually by just double clicking on it). This will give you back the Postgres app.
Finally, drag and drop the Postgres app to your Applications folder.
Now, simply double click to launch the app (i.e. to run the world’s most advanced open source database on your Mac). It just couldn’t get any easier.
You should be able to see a tiny elephant icon in your menubar. That means your database is up and running.
Tip: I suggest setting up your database to run/start automatically at Login. To do that, click on the elephant icon in your menubar and select ‘Automatically Start at Login’.
Next, you are going to want to add the
/bin directory that ships with Postgres app to your
PATH. Open your
.bash_profile (in your home directory) and add the following line to the bottom of it (if
.bash_profile doesn’t exist, simply create it by running
Restart the terminal, and run
which psql. You should get the following result:
Working directly with the database:
Now if you type
psql at a terminal prompt, you will be connected to the database server and will be able to interact with it through various commands. A list of all
psql’s commands can be found in this tutorial.
rails db command, in the context of your rails app, will do the same thing as
Personally, my preferred way to explore the db structure, tables and data is through a GUI client. At the time of this writing, my favourite client is PG Commander, but other alternatives are pgAdmin and Induction app.
Using Postgres with your Rails app:
Alright, with Postgres app successfully setup, we can now create a new Rails app, and instruct it to use PostgreSQL as database:
When the application is created, Bundler will install the
pg gem. Looking at the
database.yml file, you can see the application is already configured to use Postgres. However, you need to make some minor changes to make it work with postgres.app. Your configuration should look something like this (for development, as well as, test and production databases):
1 2 3 4 5
at the root of your rails app to create the database.
To verify that your Rails app is able to connect to your newly created database, and is able to interact with it, we are going to create a
post scaffold, run the migration, and check if we can create, retrieve, update and delete posts through the browser:
1 2 3
http://0.0.0.0:3000/posts and try creating some posts. If you can retrieve, update and delete them successfully, you are all set!