Setup PostgreSQL for Rails on a Mac
Pick your elephant poison
There are a myriad of ways to install PostgreSQL on a Mac. I will be going over brew
and Postgres.app. I like to go with the app as it eases into my workflow but does require it’s path to be set in your profile (e.g .zshrc
, .bashrc
, or fish.config
)
Homebrew Installation
The homebrew installation is straightforward and can be done by following the guide below.
- Run a brew command to install PG and take note of the documentation that follows it.
- If this is your first install run the following command to create your first database
- Create aliases to simplify the start and stop of the Postgres service
Postgres.app Installation
-
Visit https://postgresapp.com
-
Download the latest version which as of this writing is
9.2.4.1
-
Drag, drop, and open the elephant icon into the Application folder.
-
Set the following in your
.bashrc
or.zshrc
-
Open the elephant and verify it is referencing the app using
which psql
The path should look like what was entered above but if all else fails restart your terminal session.
Use Postgres in Rails
Now that you’ve successfully installed PG, you can utilize it in rails by including it in your gemfile.
New Rails App
If you have yet to generate your rails app, you can set Postgresql as your database by running rails new blog -d postgresql
You will then have to run rake db:create:all
to create the databases in the database.yml
file.
Existing Rails App
For an existing rails app you will need to add the pg
gem to your gemfile like so.
You will also need to change your database.yml file to look something like this:
Finally, you can run rake db:create:all
followed by rake db:migrat
and continue editing your amazing rails app!
Resources
No comments yet. Share on Mastodon and see your comment or write a post on your blog if you support Webmentions
No reposts yet. Share on Mastodon and see your repost or write a post on your blog if you support Webmentions
No likes yet. Share on Mastodon and see your like or write a post on your blog if you support Webmentions
No bookmarks yet. Share on Mastodon and see your bookmark or write a post on your blog if you support Webmentions
Powered by Webmentions