Assuming that you have Rails and PostgreSQL already installed on your workstation, follow the steps for a new or existing Ruby on Rails app according to your needs.
Use PostgresSQL with a new Rails app
Run the following command to create a new Ruby on Rails app using PostgresSQL adapter:
rails new myapp --database=postgresql
Now, switch into the newly created app directory and run in the terminal:
rake db:create
Add PostgreSql to an existing Ruby on Rails app
Add the following line to your gemfile, then run bundle install
gem 'pg'
Update the file
config/database.yml
to match the below code. Replace "exampleapp" instances with the name of your app.
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: exampleapp_development
test:
<<: *default
database: exampleapp_test
production:
<<: *default
database: exampleapp_production
username: exampleapp
password: <%= ENV['EXAMPLEAPP_DATABASE_PASSWORD'] %>
You will need to then recreate your database by running the following:
rake db:drop db:create db:migrate
As a shortcut, you can run the migrate reset command instead
rake db:migrate:reset
If you have a seed file, use this instead:
rake db:drop db:setup
As a shortcut, you can run the reset command instead
rake db:reset