First, you must install postgresql. On OSX, just use Homebrew, then follow the instructions.
$ brew install postgresql
If you cannot initdb
successfully (because of a Cannot allocate memory error), try running these commands first.
$ sudo sysctl -w kern.sysv.shmall=65536
$ sudo sysctl -w kern.sysv.shmmax=16777216
If that fixed it, then open or create this file /etc/sysctl.conf
, and add the following lines to give your procs more shared memory juice.
kern.sysv.shmall=65536
kern.sysv.shmmax=16777216
If the brew install postgresql
command fails, saying "Error: Failed executing: make install-world (postgresql.rb:67)" (full error output: https://gist.github.com/2998598) and you're running OSX Lion (10.7.4) and Xcode 4.3.3, you'll need to use this patch/hack to get past the make error: Homebrew/legacy-homebrew#13044
If that wasn't obnoxious enough, you may have to set your /usr/local/bin to the front of the PATH list, or you'll get a bunch of permission denied errors.
PATH=/usr/local/bin:$PATH
Unlike MySQL, Postges uses system user accounts. Typically, our apps' database configurations will leave the username and password blank, so that the user that connects to the database is the same as the user that is running the app.
If the database config calls for a specific user, such as the struck
user, adding the user to your system isn't difficult:
$ createuser struck -Ls
The -L
flag creates the user without login privileges, and the -s
flag creates the user as a superuser. If you exclude the superuser status, you'll need to add the privileges to the user's account in order to allow it to connect to your database:
[TODO]
Rails exposes a db:create
method through its rake tasks, automatically handling the creation of your app-specific database. If your app does not offer these benefits, databases can be created on Postgres like so:
$ createdb -O[username] -Eutf8 [database name]
http://robdodson.me/blog/2012/04/27/how-to-setup-postgresql-for-rails-and-heroku/