Postgresql with Ruby on Rails - 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

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

Heroku +RoR how run app locally

I'm just start working with RoR and Heroku hosting.
I want to know how can I run application cloned from Heroku?
I want to add some migration for field. Can I do it right on Heroku without cloning?
What I'm doing:
1) heroku login
2) git clone git.....
3) cd cloned_app_folder
4) bundle install
5) rails g migration add_field_to_posts field:string
On this stage I have many errors: like database.yml not exists and error in configuration.rb, /active_support/lazy_load_hooks.rb and other
6) I'm loaded db schema but it didn't help me
I dont think you can do a straight migration on Heroku.
Check and make sure you have a database.yml in the config folder.
If not and you are using postgres create one that looks something like this
development:
adapter: postgresql
encoding: unicode
database: APP_NAME_development
pool: 5
username: COMPUTER_USER_NAME
password:
test:
adapter: postgresql
encoding: unicode
database: APP_NAME_test
pool: 5
username: COMPUTER_USER_NAME
password:
production:
adapter: postgresql
encoding: unicode
database: APP_NAME_production
pool: 5
username: COMPUTER_USER_NAME
password:
If you already have the above, then run rake db:create and then rake db:migrate.
Then you should be able to run your migration.
Let me know if this doesn't solve the problem!

Creating Rails 3.1 app with Postgresql database (OSX Lion)

I cant seem to find an up-to-date guide to creating a new Rails 3.1 app with a Postgresql database. I would greatly appreciate a guide through that process.
Since Rails 3, the framework is completely database-agnostic.
What that means is, all you have to do is 2 things:
Include the pg gem in your Gemfile: gem 'pg'
Make sure your database.yml file is using postgresql as the adapter.
You can accomplish this automatically when you create a new app by appending the --database=postgresql flag:
rails new myapp --database=postgresql
But if you've already created the app, then just comment out gem 'sqlite3' in your Gemfile, add in gem 'pg', run bundle, and then change your database.yml file to use the correct adapter.
Of course, it goes without saying that you will also need to install PostgreSQL itself, but that is something you can easily find on google.
To elaborate on bricker's answer... After running:
$ rails new myapp --database=postgresql
Here is what your database.yml will look like:
development:
adapter: postgresql
encoding: unicode
database: myapp_development
pool: 5
username: myapp
password:
test:
adapter: postgresql
encoding: unicode
database: myapp_test
pool: 5
username: myapp
password:
production:
adapter: postgresql
encoding: unicode
database: myapp_production
pool: 5
username: myapp
password:
Running:
$ bundle exec rake db:create:all
will create the databases for you if you have PostgreSQL installed. The easiest way to install PostgreSQL is http://postgresapp.com/
I found you have to either add the username, in the example above - myapp, to the PostgreSQL database or change the username to an existing user.
As of Rails 4, bricker's answer does not work, at least not for me.
This command does the trick instead:
rails new new_app --d = postgres

Change from SQLite to PostgreSQL in a fresh Rails project

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.

Convert a Ruby on Rails app from sqlite to MySQL?

I made an app in Ruby on Rails and now I want to get it hosted. However, they require that I use MySQL and I set it up using sqLite3. Is there any way to convert it to use MySQL?
Step 0
To be safe, I recommend experimenting a bit with this technique in a virtual machine. Save yourself a bunch of heartache and build a virtual machine, check out your code, and have a safe playground that you can throw away if tragedy strikes.
Step 1
Make a backup copy of your database.yml file.
(from your application root)
cp config/database.yml config.database.yml.sqlite3
Step 2
Make a backup copy of your data
For Rails 3, install the YAML DB gem: https://github.com/ludicast/yaml_db
by running
gem install yaml_db
and then add to your Gemfile.
gem 'yaml_db'
For Rails 2.x install the YAML DB plugin:
script/plugin install git://github.com/adamwiggins/yaml_db.git
Run the dump task
rake db:dump
Step 3
Update your config/database.yml file. You will find entries like
development:
adapter: sqlite3
database: db/development.sqlite3
timeout: 5000
test:
adapter: sqlite3
database: db/test.sqlite3
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
timeout: 5000
Change them to
development:
adapter: mysql
encoding: utf8
reconnect: false
database: **myapp_development**
pool: 5
username: **root**
password: **supersecretpassword**
**socket: /opt/local/var/run/mysql5/mysqld.sock**
test:
adapter: mysql
encoding: utf8
reconnect: false
database: **myapp_test**
pool: 5
username: **root**
password: **supersecretpassword**
socket: **/opt/local/var/run/mysql5/mysqld.sock**
production:
adapter: mysql
encoding: utf8
reconnect: false
database: **myapp_production**
pool: 5
username: **root**
password: **supersecretpassword**
socket: **/opt/local/var/run/mysql5/mysqld.sock**
Be sure to update the values surrounded by asterix as appropriate for your platform! The socket value is only good for Mac OSX using MacPorts. Most flavors of linux do not require this value.
Step 5
If you have some errors in the following step, you might have to install the mysql or mysql2 gem:
sudo gem install mysql
or
sudo gem install mysql2
Have rake create your database
rake db:create
rake db:schema:load
Step 6
Use YamlDb to reload your data into MySql
rake db:load
As long as you have not written any SQL statements that run in sqlLite3 and not MySQL (which you won't have if all your database access is via ActiveRecord and ActiveRecord migrations) then all you need to do is change the database adapter in your database.yml config file.
Check Taps. I've successfully converted a Mysql database to Postgres with it --it should support SQLite.
Edit: Including working link from cony's comment here.
If there's no data to migrate, simply update database.yml and run 'rake db:schema:load' in the new environment. (NOT db:migrate which should only be used for incremental migrations!)
myproject user$ cd
user $ rails new myproject -d mysql
Say 'no' for all question but for Overwrite .../myproject/config/*database.yml*? (enter "h" for help) [Ynaqdh] say 'yes'.

Resources