Skip to content

Instantly share code, notes, and snippets.

@Gastove
Last active August 25, 2016 17:54
Show Gist options
  • Save Gastove/4c7a26701313fc6a387443f7cd721bc6 to your computer and use it in GitHub Desktop.
Save Gastove/4c7a26701313fc6a387443f7cd721bc6 to your computer and use it in GitHub Desktop.
Installing Postgres
# Installing Postgres
First things first: installing a database. A database is its own kind of
program; it needs to be run and managed separately from the code you write. Both
the SQL and ORM approaches require a database, run in the same way. For our work
here, we'll be using Postgres.
You have two choices: [Postgres.app](http://postgresapp.com/), or homebrew. Both do pretty much the same
thing: install a Postgres database and the `psql` CLI on your computer. Heroku's
Postgres.app makes running your database slightly less arcane; the flip side is
that installing and running your database via homebrew is more "standard" – so
these notes will assume that's what you're up to.
My friends, let us begin:
```sh
brew install postgresql
```
If all goes well, your output should end with something like:
To have launchd start postgresql at login:
ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
Then to load postgresql now:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
Or, if you don't want/need launchctl, you can just run:
postgres -D /usr/local/var/postgres
==> Summary
/usr/local/Cellar/postgresql/9.5.1: 3,118 files, 35M
I find this output a little arcane, myself, so let's do a quick breakdown of the
three things in there:
1. You can tell your Mac to turn Postgres on every time the computer turns
on. To do this, run the first command.
2. If you've decided to wire things up with command 1, Postgres will now start
when your computer boots. However, it will not be running *right now*. To make
it run right now, *if* you went with command 1, run command 2.
3. Or, you can choose to run Postgres manually, so it's only on when you need it
to be. To run Postgres manually, run the third command.
If performance isn't a concern for your laptop or if you're going to be using
Postgres *all the flipping time*, maybe do commands 1 and 2. If performance *is* a
concern for your Mac, or if you just don't like unnecessary software cluttering
things up, skip 1 and 2 and just use command 3 whenever you need it.<sup><a id="fnr.1" class="footref" href="#fn.1">1</a></sup>
Here's a handy trick to make command 3 a little nicer:
1. Make a directory under your home directory and add it to your `$PATH`. I use
`/Users/gastove/bin`. This gives you an easy, sudo-free place to put commands
on your path, where your computer can find them.
2. Make a file in your handy `bin` directory called `start-postgres`. Make the
contents of that file be this:
```sh
#!/usr/bin/env bash
postgres -D /usr/local/var/postgres
```
3. Run `sudo chmod a+x /where/you/put/the/file/start-postgres`. This makes the
file executable.
4. Now you can run postgres when/wherever you like with the `start-postgres` command.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment