The no fuss, no muss guide to installing the latest stable version of MySQL DB locally on your Mac running OS X 10.7 or later. (Hat tip to Trey Piepmeier for his excellent tutorial, upon which I improved a few things.)
Assuming you have your brew command ready to rock, make sure to run a quick brew update, telling brew to fetch the latest packages:
OK? Good. Now you need to tell brew to install MySQL by entering this command:
Enter the following two commands, one after the other (the second one starts up your new, local MySQL server and creates an initial database):
Launch MySQL Automatically
The output from that last command should instruct you to enter three additional commands. (The ones below might not be exactly what you see in your terminal. Of course, make sure you follow those instructions and not these below, unless they’re identical.):
The three commands above do the following, respectively: create a LaunchAgents folder for you if you don’t already have one, copy the mysql.plist file into that launch folder, and then loads that file into a system file so that Mac OS X starts MySQL for you each time you restart your machine. Perfect!
Start Configuring MySQL
One final (optional) step is to run the included MySQL root/user config script. It’ll step you through various default username/password/etc options that you might want to configure now that you’ve got MySQL up and running on your machine. To run this automated post-MySQL-install wizard, enter:
How to Install Homebrew on Mac OS X (10.7 or later)
This one’s super-quick and easy! If you want to easily install other tools and add-ons in the future, you need Homebrew.
Open a new shell and run the following:
It’s that simple. Really.
Homebrew Future Tip
Once Homebrew has finished installing, you’ll want to make sure to always run the following before trying to install anything using the brew command:
Running Brew’s update command instructs it to fetch the latest install recipes from its remote repository. Remember: Before you use Brew to install something, you definitely want to run the brew update command Every single time! That way, you’ll always ensure you’re installing only the latest, stable packages.
With brew installed, you’re golden. Time to install PostgreSQL!
Install PostgreSQL and Configure
With Brew, you can install PostgreSQL with the following command in Terminal:
You can now start your PostgreSQL server and create a database:
Optional You’ll need to have PostgreSQL running locally in order for your app (running in development mode, of course) to read and write to your Postgres database(s). If you want to have PostgreSQL start automatically each time you start your computer, enter the following three lines into Terminal one after another:
Done and done. PostgreSQL is up and running and now all you need to do is tweak a few setting in your Rails App’s database.yml file (in the config/ folder).
In your database.yml file, you’ll see a few environments and their respective configs beneath. Most likely you’ll see three environments: development:, test:, and production:.
For now, we’ll just change the development: environment. If you haven’t changed anything, you’ll see the following as the default config for development::
In order for your app to use your new PostgreSQL server, you’ll want to change the above to this:
You’ll want to replace name_of_your_app with the name of your app.
Editing Your Gemfile
Hold on there partner, don’t forget to tweak your Gemfile! Make sure the you’ve got the pg gem in your gemfile:
Want To Run PostgreSQL in Production?
If you want to run Postgres in your production environment as well as your development environment, make sure to add the gem 'pg' line somewhere within the :production block—and not only within your group :development, :test do block.
Finally, you’ll want to create a new database: rake db:create and you’ll probably want to run the following command to delete your tables, recreate them, and seed them with any data you may have in your seeds.db file with the following command: rake db:reset
Trying to install PostgreSQL on your Linux machine instead?
Dan Manges is crazy-smart, the CTO of Braintree, and happens to be my mentor while at Code AcademyThe Starter League. He saved me about three hours of chin-scratching, by teaching me everything below today (in about 15 minutes). Thanks man! (Probably worth noting that any errors below are courtesy of yours truly—and not Dan :)