renaud.io

Yet Another Command Line Experiments.

Rails 3: Migrate your data from SQLite to PostgreSQL

Permalink

Using my brand new enki blog instance with a SQLite database, I wrote my first post… then I told myself that it could be nice to use a PostgreSQL database, just for fun…

Google and a stackoverflow entry gives me the answer, here the step-by-step result for a RAILS_ENV="production":

  • Install PostgreSQL (see my previous post Postgresql installation on debian Wheezy)
  • $ cp config/database.yml config.database.yml.sqlite3
  • Install yaml_db: $ sudo gem install yaml_db and add that gem to your Gemfile
  • Add the PostgreSQL support: $ sudo gem install pg and add that gem to your Gemfile
  • Add a new config/database.yml.pg file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# PostgreSQL version
#   gem install pg
development:
  adapter: postgresql
  encoding: unicode
  database: blog_development
  pool: 5
  username: blog
  password: blurp
  host: localhost

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  adapter: postgresql
  encoding: unicode
  database: blog_test
  pool: 5
  username: blog
  password: blurp
  host: localhost

production:
  adapter: postgresql
  encoding: unicode
  database: blog_production
  pool: 5
  username: blog
  password: blurp
  host: localhost
  • Backup your SQLite data with yaml_db: $ rake db:dump RAILS_ENV="production"
  • Configure Rails to use your new PostgreSQL configuration: $ cp config/database.yml.pg config/database.yml
  • Create the database and the associated tables: $ rake db:create RAILS_ENV="production"
  • Load your schema: $ rake db:schema:load RAILS_ENV="production"
  • Load your data with yaml_db:$ rake db:load RAILS_ENV="production"

Et voilà !

Comments