Change from SQLite to PostgreSQL in a fresh Rails project

75,328

Solution 1

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

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

Now its become easy with the single command

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

Solution 4

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

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

Share:
75,328
Vasseurth
Author by

Vasseurth

SOreadytohelp

Updated on June 23, 2020

Comments

  • Vasseurth
    Vasseurth almost 4 years

    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?

  • Vasseurth
    Vasseurth almost 13 years
    Should I put project_test or the name of my database?
  • Chris Barretto
    Chris Barretto almost 13 years
    You can name it whatever you want. If your project name is 'calculator' I would name them calculator_production, calculator_test, calculator_development
  • sbeam
    sbeam over 11 years
    the taps gem doesn't seem to work well with 1.9.3, you might need to install 1.9.2 locally to get it to run - once I did that it was mindblowingly smooth github.com/ricardochimal/taps/issues/93
  • vich
    vich about 11 years
    I'm curious, do you actually need the username and password for configuring PG? I've only used adapter, database, and host in my Rails apps since switching from SQLite3. Is there a benefit (i.e. for security) to adding the username and password?
  • Chris Barretto
    Chris Barretto about 11 years
    @mmichael it really depends on how you have your postgres set up. Using postgres.app, brew, or native if you are on MacOS X Lion+ have different restrictions on their default setup. So if username and password do not apply, you can leave them out, or in with no values. This was just more of a 'catch all' type of configuration.
  • David Rhoden
    David Rhoden over 10 years
    What is '&TEST' doing in there (line 9)?
  • Chris Barretto
    Chris Barretto over 10 years
    "&TEST" is setting TEST as the default set of options. They can later be overridden or just left out. "<<: *TEST" is the way to access the default @DavidRhoden
  • flobacca
    flobacca over 10 years
    this is my third day on this postgresql, maybe I'm updating the database.yml file incorrectly. Do I need a migration when changing the database.yml file? I've just been changing its text and saving it.
  • Tamara
    Tamara over 10 years
    Yes, you'll need a migration after the change to database.yml, if you were using SQLite before.
  • sykaeh
    sykaeh almost 9 years
    This is no longer possible. See this question for more info: stackoverflow.com/questions/19817851/…
  • csalmeida
    csalmeida about 4 years
    This is a great answer, it changes the database.yml with the values needed. You can still go in there and change the database name according to your project.