How can I create a Postgres user and database whenever I create a new rails app?

Richard Burton picture Richard Burton · Aug 5, 2012 · Viewed 9.1k times · Source

I have followed this really helpful tutorial:

http://blog.willj.net/2011/05/31/setting-up-postgresql-for-ruby-on-rails-development-on-os-x/

I would really like to run rails new myapp and have the postgres db set up automatically. Is there any way I can do that using a Rails application template or something similar?

Answer

Gazler picture Gazler · Aug 5, 2012

On a unix based system:

sudo -u postgres createuser -d -R -P APPNAME
sudo -u postgres createdb -O APPNAME APPNAME

You can create a script and put it somewhere in your $PATH if you can't remember.