Change from SQLite to PostgreSQL in a fresh Rails project

Ruby on-RailsDatabaseSqlitePostgresqlHeroku

Ruby on-Rails Problem Overview


I have a rails app that's databases are in SQLite (The dev and production). Since I am moving to heroku, I want to convert my database to PostgreSQL.

Anyways, I heard that the local, development, database does not need to be changed from SQLite, so I don't need to change that, however, how do I go about changing the production environment from SQLite to PostgreSQL?

Has anyone ever done this before and can help?

P.S. I'm not sure what exactly this process is called, but I've heard about migrating the database from SQLite to PostgreSQL, is that what needs to be done?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

You can change your database.yml to this instead of using the out of the box sqlite one:

development:
  adapter: postgresql
  encoding: utf8
  database: project_development
  pool: 5
  username: 
  password:

test: &TEST
  adapter: postgresql
  encoding: utf8
  database: project_test
  pool: 5
  username: 
  password:

production:
  adapter: postgresql
  encoding: utf8
  database: project_production
  pool: 5
  username: 
  password:

cucumber:
  <<: *TEST

Solution 2 - Ruby on-Rails

The steps below worked for me. It uses the taps gem, created by Heroku and mentioned in Ryan Bates's Railscast #342. There are a few steps but it worked perfectly (even dates were correctly migrated), and it was far easier than the Oracle -> DB2 or SQL Server -> Oracle migrations I have done in the past.

Note that SQLite does not have a user id or password, but the taps gem requires something. I just used the literals "user" and "password".

Create the Postgres database user for the new databases

$ createuser f3
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) y

EDIT - Updated command below - use this instead

$ createuser f3 -d -s

Create the required databases

$ createdb -Of3 -Eutf8 f3_development
$ createdb -Of3 -Eutf8 f3_test

Update the Gemfile

gem 'sqlite3'
gem 'pg'
gem 'taps'
$ bundle

Update database.yml

#development:
#  adapter: sqlite3
#  database: db/development.sqlite3
#  pool: 5
#  timeout: 5000

development:
  adapter: postgresql
  encoding: unicode
  database: f3_development
  pool: 5
  username: f3
  password:
  
#test:
#  adapter: sqlite3
#  database: db/test.sqlite3
#  pool: 5
#  timeout: 5000

test:
  adapter: postgresql
  encoding: unicode
  database: f3_test
  pool: 5
  username: f3
  password:

Start the taps server on the sqlite database

$ taps server sqlite://db/development.sqlite3 user password

Migrate the data

$ taps pull postgres://f3@localhost/f3_development http://user:password@localhost:5000

Restart the Rails webserver

$ rails s

Cleanup the Gemfile

#gem 'sqlite3'
gem 'pg'
#gem 'taps'
$ bundle

Solution 3 - Ruby on-Rails

Now its become easy with the single command

bin/rails db:system:change --to=postgresql

Solution 4 - Ruby on-Rails

Since you're moving to heroku, you can use taps to do this:

heroku db:push

This will push your local development sqlite data to production, and heroku will automagically convert to postgres for you.

This should also work to push a production sqlite db to heroku, but it's not tested.

RAILS_ENV=production heroku db:push

Solution 5 - Ruby on-Rails

you will also need to add the line "gem 'pg'" to your gemfile, 'pg' being the current postgres gem for Rails.

Solution 6 - Ruby on-Rails

Simply update the config/database.yml file:

default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5

development:
  <<: *default
  database: projectname_development

test:
  <<: *default
  database: projectname_test

production:
  <<: *default
  database: projectname_production
  username: 
  password: 

The above is what's generated when you run:

$ rails new projectname --database=postgresql --skip-test-unit

Also add this to your Gemfile:

gem 'pg'

Solution 7 - Ruby on-Rails

Just Update you datatbase.yml

development: &development
  adapter: postgresql
  database: Your_database_name
  username: user_name
  password: password
  host:     localhost
  schema_search_path: public
  min_messages: warning

test:
  <<: *development
  database: test_database_name

production:
  <<: *development
  database: production_db_name

We are using rails and the basic standards should be follow like DRY, Convention over Configuration etc.. so in above code we are not repeating same code again and again.

Solution 8 - Ruby on-Rails

It's been mentioned above me, but I don't have enough reputation as a lurker to be able to upvote it. In the hopes of drawing a little more attention for Rails newbies reading this answer:

> you will also need to add the line "gem 'pg'" to your gemfile, 'pg' being the current postgres gem for Rails.

^^^ This is a key piece in addition to the database.yml file described in the selected answer to migrate your Rails app to Postgres.

Solution 9 - Ruby on-Rails

After replacing gem 'sqlite3 with gem pg in the gemfile, I kept getting the sqlite3 error when pushing to Heroku master because I forgot to commit the updated gemfile. Simply doing the following solved this:

git add .
git commit -m 'heroku push'
heroku create 
git push heroku master

Solution 10 - Ruby on-Rails

This is how I have mine setup. If you are only using MRI and not Jruby you can skip the logic in the adapter settings.

defaults: &defaults
  adapter: <%= RUBY_ENGINE == 'ruby' ? 'postgresql' : 'jdbcpostgresql' %>
  encoding: unicode
  pool: 5
  timeout: 5000

development:
  database: project_development
  <<: *defaults

test:
  database: project_test
  <<: *defaults

production:
  database: project_production
  <<: *defaults

Solution 11 - Ruby on-Rails

You can try following: sqlite3 development.db .dump | psql dbname username

or try with sqlitetopgscript: http://trac-hacks.org/browser/sqlitetopgscript/0.10/sqlite2pg

Solution 12 - Ruby on-Rails

A possible solution (not for heroku) it's to use yaml.db from:

http://www.railslodge.com/plugins/830-yaml-db

Solution 13 - Ruby on-Rails

Today I had the same issue. I'm working on Rails 4.2.8. The solution was specify the pg gem version, in my case, 0.18.4.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVasseurthView Question on Stackoverflow
Solution 1 - Ruby on-RailsChris BarrettoView Answer on Stackoverflow
Solution 2 - Ruby on-Railsport5432View Answer on Stackoverflow
Solution 3 - Ruby on-RailsK ABHIRAMView Answer on Stackoverflow
Solution 4 - Ruby on-RailsJesse WolgamottView Answer on Stackoverflow
Solution 5 - Ruby on-RailsGus ShortzView Answer on Stackoverflow
Solution 6 - Ruby on-RailsjungledreView Answer on Stackoverflow
Solution 7 - Ruby on-RailssunilView Answer on Stackoverflow
Solution 8 - Ruby on-RailsJustin HoukView Answer on Stackoverflow
Solution 9 - Ruby on-RailsZorakView Answer on Stackoverflow
Solution 10 - Ruby on-RailsianksView Answer on Stackoverflow
Solution 11 - Ruby on-RailsVibhor KumarView Answer on Stackoverflow
Solution 12 - Ruby on-RailsF.FilippiView Answer on Stackoverflow
Solution 13 - Ruby on-RailsBenjamin Peña OlveraView Answer on Stackoverflow