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
My ruby on rails app contains two database connection and its working good in localhost, but its not working on heroku. Getting this error
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/connection_specification.rb:52:in 'resolve_hash_connection': database configuration does not specify adapter (ActiveRecord::AdapterNotSpecified)
database.yml
production:
adapter: mysql2
encoding: unicode
database: first_database
pool: 5
username: root
password:
production:
adapter: mysql2
encoding: unicode
database: second_database
pool: 5
username: root
password:
Since heroku overwrites default database.yml file, I followed this tutorial https://roratmindfiresolutions.wordpress.com/2013/05/27/connect-to-remote-database-server-from-heroku to setup multiple database connection on heroku, but still no change. I am using rails 4.0.1 . Any helps and suggestions are really appreciable. Thanks.
Note that in his YAML file he calls them production and production_sec whereas you're using production twice.
I have a weird problem.
I have set my database.yml file to use sqlite3 for all three (production, test, dev) databases
I create a new rails project with all defaults.
I fire up rail server using WEBrick
I get "ActiveRecord::ConnectionNotEstablished" error
I try $rake db:create
I get the following error:
specified 'postgresql' for database adapter, but the gem is not loaded. Add gem 'pg' to your Gemfile.
I install pg and postgres server and I get
fe_sendauth: no password supplied Error on the webpage
I try $rake db:create again on the console and get
fe_sendauth: no password supplied (which I know is a postgres password error)
It seems that Rails is choosing a different database adapter than in my database.yml file.
I don't know where it could be. It even seems to be looking for a specific database that I used in some previous project. Therefore Rails must be looking at someother config file.
Can someone help.
Add sqlite3 gem to your Gemfile:
gem 'sqlite3'
and set sqlite3 as adapter in your database.yml:
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
I'm using Ruby on Rails ( rails version 2.3.4), and using pg gem (version 0.12.2) on Windows.
My database.yml file has the following content:
production:
adapter: postgresql
encoding: utf8
host: localhost
database: canvas4
username: canvas
password: canvas
port: 5433
timeout: 5000
pool: 5
queue:
adapter: postgresql
encoding: utf8
database: canvas4_queue
host: localhost
port: 5433
username: canvas
password: canvas
timeout: 5000
pool: 5
But when i run command: bundle exec rake db:initial_setup RAILS_ENV=production, the error occurs:
Invalid connection "adapter"
I've also tried with gem "activerecord-postgresql-adapter" but the same error occurs as above.
I can successfully setup with sqlite3 driver.
Do you know how to get postgresql adapter to work with rails 2.3 ?
make sure your db-adapter in database.yml is set to "postgresql". If still you have the same problem, then try using the pure-ruby adapter:
gem install postgres-pr
(make sure you uninstall the pg gem before... to avoid conflicts)
If it works, then it's a problem with the postgres gem. Anyway, for development purposes, using the pure-ruby gem is usually ok.
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.