ROR change application database from SQLite to PostgreSQL - ruby-on-rails

I have a web application which uses SQLite. I deploy it on heroku which uses PostgreSLQ. This causes problems sometimes and I was advised to develop my app using PostgreSQL instead of SQLite.
I found out that I should modify database.yml like that (same for test and production):
development:
adapter: postgresql
database: my_database
username: my_username
password: my_passwod
host: /var/run/postgresql or localhost
Well the only database I've ever used is SQLite, so I just tried to take my chances, but failed. I filled this file with some random data.
rake db:migrate resulted in:
When I used host: localhost
> could not connect to server: Connection refused Is the server running
> on host "localhost" and accepting TCP/IP connections on port 5432?
When host: /var/run/postgresql
> could not connect to server: No such file or directory
> Is the server running locally and accepting connections on Unix domain socket
> "/var/run/postgresql/.s.PGSQL.5432"?
I suppose I should start PostgreSQL server first, but have no idea how to do this. Please give me a step by step answer how to move from a SQLite application to a working PostgreSQL application.

I would like to advise to you that you should download Postgresql including the PGADMIN itself which is easier to use than the psql terminal.
And I think when you download/install Postgresql from their official website... the package was complete already.
Upon installing, the postgresql will ask you a certain password that you will be using in accessing your postgresql server.
After the installation, open the PGADMIN and connect to the server. Enter your password (which you had declared during installation).
If you can't connect to the server, then edit the port. To do this, right click the server then go to properties... edit the port into something which is free. Example: 5433 and so on. It's up to you.
If everything's finally working... setup the correct config for your database.yml
This is important:
development:
adapter: postgresql
database: name_of_database_here
host: localhost
username: postgres
password: your_db_server_password_here
pool: 5
timeout: 5000
port: 5433
Okay from that config info above, specify the important parts. By default, your db server username is postgres and obviously your host is localhost because you are setting up under the development.
If your port is 5432 by default then just remove the port part.
Let's go to your gemfile.
In order for you to deploy your app in heroku. Use gem 'pg' instead of sqlite3.
If you have an existing sqlite3 database then put the gem inside the development group. In that case, Heroku will successfully bundle during git push heroku master process.
group :development do
gem 'sqlite3'
end
Your gem 'pg' can either go outside the groups or put it in your production group.
Important:
Before any deployment procedure, make sure that you can run the app locally (localhost). Then if everything's working... that's the time that you should organize the necessary stuffs appropriately.
If you wish to switch to Postgresql instead of sqlite3 after pushing the app to Heroku... you can do so by pgbackups add-on and pg_restore the dump file into your local postgresql db server.
That's it. Hope it helps.

Take a look on this guide https://www.digitalocean.com/community/tutorials/how-to-set-up-ruby-on-rails-with-postgres so you can recreate the process manually by changing your database.yml and Gemfile.
If you need to migrate your database information run
pgloader ./production.sqlite3 postgres://username:password#localhost/database
Check https://migara.li/2017/07/24/migrating-from-sqlite-to-postgres/
Other alternatives like taps aren't working right now
http://railscasts.com/episodes/342-migrating-to-postgresql

Related

Rails fails to connect to google cloud sql using proxy

I'm deploying a rails 5 app using Google Cloud SQL(postgresql) & App Engine and I am having issues with the proxy. I followed the tutorial here Ruby on Rails Cloud Sql and setup the proxy successfully.
From my local machine:
psql -h "/cloudsql/[CONNECTION:NAME]" --user [USER] --password
I can see all remote databases and the connections being handled by proxy in the other window. Next I try to run rake db:migrate and get the following error.
PG::ConnectionBad: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/cloudsql/[CONNECTION:NAME]/.s.PGSQL.5432"?
Here is my database.yml
default: &default
adapter: postgresql
encoding: utf8
pool: 5
timeout: 5000
username: [USER]
password: [USER:PASS]
host: /cloudsql/[CONNECTION:NAME]
Since the proxy is working from the local machine it must be something in my rails app or pg gem. I've reinstall the PG gem with no change. How can get I get my rails app to use the proxy successfully?
Did you activate the Cloud SQL API?
In my case I had forgotten that. Enable that one in your API console. Then it worked for me.

Rails development environment trying to connect to AWS database, not localhost's PostgreSQL

I have an existing Rails app which was previously running successfully on my local laptop (running OSX) with the database stored locally in PostgreSQL. I've also successfully deployed the Rails app to Elastic Beanstalk with the database in RDS - the hosted site is still running.
I haven't touched the localhost version for months and tried to start it today using rails server -e development, but when visiting http://localhost:3000/ as per usual, I get a PG error message
PG::ConnectionBad at / FATAL: password authentication failed for user
"murjfphxxxxxx" FATAL: no pg_hba.conf entry for host "115.x.x.x",
user "murjfphxxxxxx", database "d37vsvehxxxxxx", SSL off
The same error is thrown in the Rails console. It seems that the development version is trying to connect to the AWS database, rather than the localhost database, but I can't figure out why.
/config/database.yml lists the development environment as:
development:
adapter: postgresql
encoding: unicode
database: <my-app>_development
host: localhost
pool: 5
username: postgres
password: <password>
I can connect to the localhost PG database via PSQL and see the contents, so it appears that the localhost PG database is valid.
Why would the development environment try to connect to the AWS version of the database? Please let me know if you need any further information to debug this.
Using ruby-2.3.1, Rails 4.2.0, PostgreSQL 9.6.0.0
You probably have DATABASE_URL in your env. You can check by typing env | grep DATABASE_URL in terminal.
If so, try unsetting it with unset DATABASE_URL.
As said in documentation:
If you have both config/database.yml and ENV['DATABASE_URL'] set then
Rails will merge the configuration together. To better understand this
we must see some examples.
When duplicate connection information is provided the environment
variable will take precedence.

Cannot connect to server when using nitrous

I'm using nitrous on a PC (Windows 7), and having trouble connecting to the server (using ruby on rails). It worked perfectly yesterday (same project, same computer).
The error I'm getting:
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
My database.yml file:
development:
adapter: postgresql
database: mvp_development
test:
adapter: postgresql
database: mvp_test
I've confirmed that my Gemfile does contain 'pg', tried running bundle update and bundle install, uninstalling pg and running bundle install, and running gem pristine pg.
Make sure you're starting the postgresql server. If your box has been shutdown then the process will get killed so you need to restart.
You can do this using the parts start postgresql command in the console.
There will be paid plans in the near future that allow you to keep your box alive indefinitely so you won't need to restart postgresql and other services everytime you sign in.

How to migrate a postgresql database

We had a developer developed a website under his local Mac, and the developer just transferred the zip file of rails site to me, but I am using Windows.
Could anyone help me over the steps on how to change from his database to my local postgresql database ? Sorry I am new at this.
So far:
1) I've edited the database.yml file, host is localhost port 5432 and database name is data1, so I went into pGAdminII and create the same database name.
2) I've created a super user and supplied the credential to database.yml.
Is that all I need to do ? Do I need to setup anything else ? thanks!
You'll need to change the adapter in the config/database.yaml file from whatever it was to postgres, something along the lines of:
development:
adapter: postgresql
database: data1
host: localhost
username: someone
password: something
You will also need to ensure you have the relevant gem in your gemfile.
gem 'pg'
That should just about do the trick. Run your rake db:version to test the connection, if all seems well go for a rake db:migrate. Post any errors you get.

Rails: Deploying to Heroku, Many Problems

Trying to deploy my rails app to Heroku for the first time, I ran into many problems. The app crashed, and you can look at the logs in the image: http://i.stack.imgur.com/bsx1b.png
Even worse, when I tried to look at my application locally, it failed to work on postgreSQL environment.It worked fine when I went back to use sqLite3 in database.yml. I thought the problem might be that I actually have to install postgreSQL, in addition to adding pg gem and running bundle install.
I went ahead to install PostgreSQL with the one-click installer (Windows 7 64bit). After reboot, a simple rails server or bundle install commands started failing, so I reinstalled the entire ruby & rails.
Now rails server command works fine, and it prompts a different error when I try to look at my app on local environment. So right now I have pgAdmin III and Rails
PG::Error
fe_sendauth: no password supplied
I tried to follow this, but I couldn't find "pg_hba.conf." I guess the answer was based on a different OS.
And now again, the bundler is not working, giving me errors...
Questions:
1) Was I right to install postgreSQL with the one-click installer? Because this caused my ROR to "crash" somehow, and I had to reinstall rails altogether. 2) I think I am going to delete everything related to postgreSQL, reinstall Rails, and start everything from beginning. What are the steps that I have to take? All the references I've looked at do not seem to fit Windows environment.
In my gemfile, I have
gem 'pg'
gem 'thin'
In my database, I have
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
development:
adapter: postgresql
encoding: utf8
database: mangfeel_development
pool: 5
timeout: 5000
# 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: utf8
database: mangfeel_test
pool: 5
timeout: 5000
production:
adapter: postgresql
encoding: utf8
database: mangfeel_production
pool: 5
timeout: 5000
Errors I'm getting at this moment, after installing PostgreSQL:
c:\ruby\myapp>rails server
C:/Program Files (x86)/ruby-1.9.2/lib/ruby/site_ruby/1.9.1/rubygems.rb:926:in `report_activate_error': Could not find RubyGem railties (>= 0) (Gem::LoadError)
from C:/Program Files (x86)/ruby-1.9.2/lib/ruby/site_ruby/1.9.1/rubygems.rb:244:in `activate_dep'
from C:/Program Files (x86)/ruby-1.9.2/lib/ruby/site_ruby/1.9.1/rubygems.rb:236:in `activate'
from C:/Program Files (x86)/ruby-1.9.2/lib/ruby/site_ruby/1.9.1/rubygems.rb:1307:in `gem'
from C:/RailsInstaller/Ruby1.9.3/bin/rails:18:in `<main>'
When I run bundle install:
Gem::InstallError: The 'json' native gem requires installed build tools
Please update your PATH to include build tools or download the DevKit
from 'http://rubyinstaller.org/downloads' and follow the instructions
at 'http://github.com/oneclick/rubyinstaller/wiki/Development-Kit'
An error occurred while installing json (1.7.5), and Bundler cannot continue.
Make sure that `gem install json -v '1.7.5'` succeeds before bundling.
So I will just remove pgAdminIII, PostgreSQL, Ruby on Rails entirely, and start from scratch.
I thought deploying wasn't going to be this hard, but moving from SQLite3 to PostgreSQL is taking more time and effort than I thought it would. I would really appreciate some help on this problem.
------------------------------------------------------------------------------------------------------------------------
UPDATE: AFTER REINSTALLING EVERYTHING, I'M DOING THE SETUP AGAIN FROM SCRATCH.
I checked that my SQLite3 version app worked fine. So I moved onto setting up postgreSQL. I got the following error when I first set up my postgreSQL. By setting up, I mean editing config/database.yml and installing the gem 'pg' and removing the gem 'sqlite3.' When I tried to connect to localhost:3000, I got the following error:
ActiveRecord::ConnectionNotEstablished
After some research, I found that there could be more steps to setting the db. So I ran the bundle command,
bundle exec rake db:setup
But then in the console, I got the following error when I ran bundle exec rake db:setup.
Couldn't create database for {"adapter"=>"postgresql", "host"=>"localhost", "encoding"=>"utf8", "database"=>"db/myapp_test", "pool"=>5, "timeout"=>5000}
rake aborted!
could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
c:/ruby/myapp/config/environment.rb:5:in `<top (required)>'
Tasks: TOP => db:schema:load => environment
When I tried to connect to localhost:3000, I got the above error again, instead of ActiveRecord::ConnectionNotEstablished.
Question: What did I do wrong, and how do I solve the problem?
If you have error:
PG::Error
fe_sendauth: no password supplied
You have good news, your app can work with PostgreSQL database now. (But you need to configure some thing)
After installing PostgreSQL, you can create new rails app using PostgreSQL by command:
rails new my_app -d postgresql
This is database.yml will be created:
development:
adapter: postgresql
encoding: unicode
database: my_app_development
pool: 5
username: my_app
password:
test:
adapter: postgresql
encoding: unicode
database: my_app_test
pool: 5
username: my_app
password:
production:
adapter: postgresql
encoding: unicode
database: my_app_production
pool: 5
username: my_app
password:
You see that, three database has username and password field, this is account user in Postgresql, three database in database.yaml file will created by this user. So you need to provide username and password for it. Default after install Postgresql, it created a user with username is postgresql and password is postgresql. You can use it.
If you want to use other user, just open pgAdmin III and create new user by right click on Login Roles and choose New login role to create new user with password. Then put that username and password to database.yaml file.
After that, all you need now, is find and open pg_hba.conf file to make some configure for app can work with PostgreSQL. I'm not using Windows for a long time, so I'm not sure where it is, but you can find in folder you installed PostgreSQL. Example, if you install on E:\ partition, maybe you can find it in:
E:\PostgreSQL\version\data
After you found it, open and find a line:
# "local" is for Unix domain socket connections only
local all all ident sameuser
Change it to:
# "local" is for Unix domain socket connections only
local all all md5
Then restart your Postgresql database. To create database for app, run rake db:create or rake db:create:all. Now your app can using PostgreSQL database now, so happy :).
I am not an expert on Rails, but here is one of my YAML files for a recent deployment i did on heroku with POSTGRES
development:
adapter: postgresql
host: localhost
database: dbname_development
production:
adapter: postgresql
host: localhost
database: dbname_production
Seems like you are missing a parameter called host in case of postgres deployment
Steps to Move From SQLite3 to PostgreSQL on Windows, for an Existing Rails App to Deploy To Heroku:
In your Gemfile, find gem 'sqlite3' and change it to gem 'pg'. Run bundle install.
Download Postgres from here. Choose the one-click installer and select your OS environment (32 or 64 bit). During installation, do NOT choose to install any extra software it recommends you to install. It will possibly mess up your existing Rails and you will have to reinstall Rails.
Now go to config/database.yml, and change it to the following format. The current one is set for SQLite3 and is missing some fields necessary for PostgreSQL.
development:
adapter: postgresql
encoding: unicode
database: my_app_development
pool: 5
username: my_app
password:
test:
adapter: postgresql
encoding: unicode
database: my_app_test
pool: 5
username: my_app
password:
production:
adapter: postgresql
encoding: unicode
database: my_app_production
pool: 5
username: my_app
password:
The adapter is different, and there are fields for username and password. The default username and password is postgresql, but if it doesn't work (it didn't work for me), then go to your pgAdmin, create a new login role to make another username/password (check the option to make it a super user) and use that.
4, At this point, you can create a new app by running
rails new my_app -d postgresql
Run your local server, and try connecting to localhost:3000 to see if the welcome page is showing.
5, If everything is working fine, then all you need to do is create a new PostgreSQL database, and migrate your existing models to it. Run
rake db:create:all
rake db:migrate
Done! Now check if your existing app is working fine locally, and try deploying your app to Heroku.

Resources