开发者

Creating Rails 3.1 app with Postgresql database (OSX Lion)

开发者 https://www.devze.com 2023-04-12 18:07 出处:网络
I cant seem to find an up-to-date guide to creating a new Rails 3.1开发者_StackOverflow社区 app with a Postgresql database. I would greatly appreciate a guide through that process.Since Rails 3, the f

I cant seem to find an up-to-date guide to creating a new Rails 3.1开发者_StackOverflow社区 app with a Postgresql database. I would greatly appreciate a guide through that process.


Since Rails 3, the framework is completely database-agnostic.

What that means is, all you have to do is 2 things:

  1. Include the pg gem in your Gemfile: gem 'pg'
  2. Make sure your database.yml file is using postgresql as the adapter.

You can accomplish this automatically when you create a new app by appending the --database=postgresql flag:

rails new myapp --database=postgresql

But if you've already created the app, then just comment out gem 'sqlite3' in your Gemfile, add in gem 'pg', run bundle, and then change your database.yml file to use the correct adapter.

Of course, it goes without saying that you will also need to install PostgreSQL itself, but that is something you can easily find on google.


To elaborate on bricker's answer... After running:

$ rails new myapp --database=postgresql

Here is what your database.yml will look like:

development:
  adapter: postgresql
  encoding: unicode
  database: myapp_development
  pool: 5
  username: myapp
  password:

test:
  adapter: postgresql
  encoding: unicode
  database: myapp_test
  pool: 5
  username: myapp
  password:

production:
  adapter: postgresql
  encoding: unicode
  database: myapp_production
  pool: 5
  username: myapp
  password:

Running:

$ bundle exec rake db:create:all

will create the databases for you if you have PostgreSQL installed. The easiest way to install PostgreSQL is http://postgresapp.com/


I found you have to either add the username, in the example above - myapp, to the PostgreSQL database or change the username to an existing user.


As of Rails 4, bricker's answer does not work, at least not for me.

This command does the trick instead:

 rails new new_app --d = postgres
0

精彩评论

暂无评论...
验证码 换一张
取 消