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?

1
  • 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. Commented Aug 5, 2012 at 20:38

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

You shouldn't need sudo for those command - only a "superuser" account for the Postgres database.
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) postgresql.org/docs/9.1/static/app-createuser.html createdb -O will create a db with the specified owner (-O) postgresql.org/docs/9.1/static/app-createdb.html
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

3 Comments

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.
You can create a database template... I haven't tried yet, but there is a railscast about it.
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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.