Change from SQLite to PostgreSQL in a fresh Rails project - ruby-on-rails

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?

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

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

Now its become easy with the single command
bin/rails db:system:change --to=postgresql

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

you will also need to add the line "gem 'pg'" to your gemfile, 'pg' being the current postgres gem for 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'

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.

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.

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

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

You can try following:
sqlite3 development.db .dump | psql dbname username
or try with sqlitetopgscript:
http://trac-hacks.org/browser/sqlitetopgscript/0.10/sqlite2pg

A possible solution (not for heroku) it's to use yaml.db from:
http://www.railslodge.com/plugins/830-yaml-db

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.

Related

How do I change rails' default database from sqlite3 to postgres, across all new projects?

Apologies for the rookie question. I would like to make postgresql my default for all new rails apps. I'm aware of the command:
rails new my_app --database=postgresql
...but I have an irrational dislike for sqlite3 and for typing this extra command. I want my rails apps to love postgres monogamously, without me telling them they shouldn't hook up with sqlite3 first. How do I go about this?
I use rbenv (again, irrationally) to manage my ruby versions. Thanks in advance.
Create a .railsrc file in your HOME directory and put your db override there
# ~/.railsrc
--database=postgresql
You can add all other overrides that you might want to use, like --skip-test-unit or the like.
This file will be applied each time you run a rails new command.
you can change default database by changing database.yml according to given file and don't forget to add pg gem in route gemfile like this gem 'pg'
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:
if this did'nt help you see the rails cast Migrating to PostgreSQL

ActiveRecord::ConnectionNotEstablished for new project

I made a wrong choice and now I am sticked at the middle of my project
I used ruby 5.0.0rc1, and while I start my project with rubymine I get "ActiveRecord::ConnectionNotEstablished"
I also get an error message while I type rake db:create :
Gregoires-MacBook-Pro:p1 gregoire$ rake db:create
Rake tasks not supported by 'pg' adapter
Couldn't create database for {"adapter"=>"pg", "pool"=>5, "timeout"=>5000, "database"=>"pg"}
rake aborted!
ActiveRecord::Tasks::DatabaseNotSupported: Rake tasks not supported by 'pg' adapter
my database/yml :
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
default: &default
adapter: 'pg'
pool: 5
timeout: 5000
development:
<<: *default
database: 'pg'
test:
<<: *default
database: 'pg'
production:
<<: *default
database: 'pg'
I am newbie with ror , any help will be appreciated (please detail)
greg
config/database.yml gives Rails the information needed to access the database of your choice.
The adapter name pg is not valid. If you intend to use PostgreSQL, you should set it as postgresql.
And you have to make sure PostgreSQL is installed and running.
A typical database.yml could be like the following:
default: &default
adapter: postgresql
encoding: unicode
pool: 5
timeout: 5000
username: your_username_for_postgresql
password: your_password_for_postgresql
development:
<<: *default
database: your_app_name_development
test:
<<: *default
database: your_app_name_test
production:
<<: *default
database: your_app_name_production
username: your_username_for_postgresql_on_production
password: your_password_for_postgresql_on_production
Thanks for your answers, I had multiple problems :
1/ Postgresql was not properly installed AND running
Fresh install using some comments found here :
https://gist.github.com/lxneng/741932
2/ my database .yml was nos properly configured, I created users with
new access : http://www.cyberciti.biz/faq/howto-add-postgresql-user-account/
What you gave me as advise made me find the way, also a project 'out of the box' was not runable
Thanks for your time and advises
greg

Postgresql with Ruby on Rails

I want to migrate from sqlite3 to postgresql. After doing search I found that I should change my database.yml to somthing like that:
adapter: postgresql
encoding: unicode
database: [insert your dev database name]
pool: 5
username: [insert your user name]
password:
But I don't know what to provide in place of database, username and password. Because i don't remember creating any of them. I just created my rails app and migrations , and thats it.
To configure your rails project with postgres database
do the below steps.
provide your database details in the database.yml file
eg:
development:
username: postgres
database: ur-db-dev
password: pass
encoding: UTF8
adapter: postgresql
timeout: 500
pool: 5
add postgres gem in your Gemfile
gem 'pg'
then,
Do bundle install to install postgres gem
rake db:create to create your database
rake db:migrate to migrate your migration files

recompiling database.yml file with gemfile? ruby on rails

I'm new to Ruby on Rails and postgreSQL and had a question. Does the database.yml file get compiled when you run bundle install on a Gemfile? Initially my gemfile had sqlite3, but I changed it to pg and tried to run bundle install again to recompile the database.yml file, but the file still says it's using SQLite.
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
I want the adapter to be switched to postgresql and I believe some other fields should be switched when using pg, but I'm unsure. Can anyone clarify this for me, thank you.
No, database.yml is not recompiled automatically. When you change the gem in Gemfile, you need to also change the file:
development:
adapter: postgresql
database: dbname
username: user
password: password
encoding: unicode
you need to replace too your the adapter value in your config/database.yml
When you run bundle install it will install the gems declared in your Gemfile, but you must manually setup you database configuration. Bundle don't compile your anything on your application except the Gemfile.lock where are declared the gems versions.
Something like:
development:
adapter: postgresql
encoding: unicode
database: myapp_development
host: localhost
password: password # if you need a password
test:
adapter: postgresql
encoding: unicode
database: myapp_test
host: localhost
password: password # if you need a password
production:
adapter: postgresql
encoding: unicode
database: myapp_production
host: localhost
password: password # probably you will need a password

Rails Change Default Projects Database 'sqllite' to PostgreSql

I'm learning ruby. I've created some application and played with it.
i wanted to change the database to postgres. I modified gemfile with
gem 'pg'
[pg already installed in usr/bin/pg]
then update database.yml with
postgresql where sqllite is used.
pg connection bad:
FATAL: role "user" does not exist
i've created some postgres user already. what should i have to change to work with postgres?
development:
adapter: postgresql
database: db/development.postgresql
pool: 5
timeout: 5000
user:
adapter: postgresql
database: db/test.postgresql
pool: 5
timeout: 5000
production:
adapter: postgresql
database: db/production.postgresql
pool: 5
timeout: 5000
If you don't specify a username to connect to PostgreSQL as, libpq will connect using the login username of the current running user.
libpq is what the Pg gem used by Rails uses to connect to PostgreSQL.
So, in your database.yml, specify the PostgreSQL user to connect to the database as with a user: entry. Make sure this corresponds to a user that exists in PostgreSQL, and that pg_hba.conf permits this user to connect.
To learn more, see the client authentication chapter of the PostgreSQL docs.
I know its a two year old question, but I just ran into the same problem, and i fixed it by changing the syntax of config.yml, then I rake db:migrate
default: &default
adapter: postgresql
encoding: unicode
pool: 5
development:
<<: *default
database: yourappnamehere_development
test:
<<: *default
database: yourappnamehere_testdevelopment:
production:
<<: *default
database: YOURAPPNAME_production
username: YOURUSERNAME
password: <%= ENV['YOURAPPNAME_DATABASE_PASSWORD'] %>

Resources