28

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

3
  • do you want installation guideline as well? Commented Oct 12, 2011 at 14:16
  • 1
    no, just what has to happen after "rails new appname --database=postgresql" Commented Oct 12, 2011 at 14:20
  • See my answer - run bundle and you're finished. Commented Oct 12, 2011 at 14:23

4 Answers 4

69

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.

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

6 Comments

could you please elaborate on how to: "change your database.yml file to use the correct adapter"
Try the rails new command posted in the answer and take a look at the generated database.yml file. (It shows: adapter: postgresql)
where is the database located within the app? for instance, if i want to access it via a GUI, where is the file i need to load?
Postgres is a complete database server, sort of like MySQL. There's not a single file you can load to see it and the data isn't stored in your application anywhere. Check here for Postgres GUI options.
If you want to locate the database.yml file it should be in your_app/config/database.yml
|
23

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/

Comments

0

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.

Comments

0

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

Comments

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.