7

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?

iltempo
  • 15,718
  • 8
  • 61
  • 72
Richard Burton
  • 2,230
  • 6
  • 34
  • 49
  • I suppose I could create a user and database that I use for all my little test applications. However for larger stuff I obviously want separate databases. – Richard Burton Aug 05 '12 at 20:38

2 Answers2

15

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.

Gazler
  • 83,029
  • 18
  • 279
  • 245
  • 1
    You shouldn't need sudo for those command - only a "superuser" account for the Postgres database. –  Aug 05 '12 at 21:08
  • 5
    Just for completion sake, `createuser -d -R -P will create a user that can create a db (-d), can't create role (-R), and prompt for password(-P)` http://www.postgresql.org/docs/9.1/static/app-createuser.html `createdb -O will create a db with the specified owner (-O)` http://www.postgresql.org/docs/9.1/static/app-createdb.html – zznq Oct 17 '13 at 22:51
9

Actually you don't need to create a new user each time you create a new rails app.

All you have to do is create the new application and change the username in your database.yml

development:
  adapter: postgresql
  encoding: unicode
  database: newapp_development
  pool: 5
  username: #your username
  password:
...

than just:

rake db:create:all
gabrielhilal
  • 10,660
  • 6
  • 54
  • 81
  • Thanks for that. I didn't realise that. Is there anyway I can customise that database.yml file as it's created? I know it's two seconds to change it but I just like being able to quickly try out different ideas. – Richard Burton Aug 05 '12 at 20:47
  • 1
    You can create a database template... I haven't tried yet, but there is a [railscast](http://railscasts.com/episodes/148-app-templates-in-rails-2-3) about it. – gabrielhilal Aug 05 '12 at 23:15
  • Really great screencast. I want to create a template.rb file with `'config/database.yml', %Q{stuff for database.yml here}` However I need to get the name of the app into the template.rb file. Is that possible? – Richard Burton Aug 06 '12 at 07:40