Rails fails to connect to google cloud sql using proxy - ruby-on-rails

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.

Related

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.

Run Postgresql on Windows 7 with Ruby on Rails

I have installed pg on windows 7 and have an existing ruby on rails application that uses sqlite3 and want to change it to postgresql so I can deploy. I also downloaded pgAdmin but I cant start a server from it but I get the following error:
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?
So i edited database.yml in my rails application to be
development:
adapter: postgresql
database: my_database_development
pool: 5
timeout: 5000
and after I bundle and it doesnt give an error so pg is installed i try to rake db:create but I also get the same error as above.
but in my services.msc I cant find postgresql-[version] so I tried to start the postgresql service by running cmd in administrator
C:\Windows\system32>NET START postgresql-x64-9.1
The service name is invalid.
C:\Windows\system32>NET START postgresql
The service name is invalid.
What should I do?
You need to add:
host: localhost
under development

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.

ROR change application database from SQLite to PostgreSQL

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

How to Use Mongoid on Rails?

I've been trying to use Mongoid with Rails on Ubuntu. I have installed mongoDB via apt-get. But when I try to run the rails server, I get this error:
/home/myusername/.rvm/gems/ruby-1.9.2-p180#mysite/gems/mongo-1.3.1/lib/mongo/connection.rb:518:in
`connect': Failed to connect to a
master node at localhost:27017
(Mongo::ConnectionFailure)
I am just new to using Mongoid, any help will be appreciated. Thanks a lot in advance!
As Piotr says you need to start the Mongo DB server. If you want to quickly test this, run mongod from a shell.
You need to start MongoDB server. I don't know exactly how to manage services in Ubuntu, but somewhere in /etc/rc.d or /etc/init.d you should have the mongodb script, run it with 'start' argument. I think the default configuration should do for your setup, but check if you have config/mongoid.yml in your Rails app. It should contain something like:
development:
host: localhost
database: app_development
test:
host: localhost
database: app_test
# set these environment variables on your prod server
production:
[...]

Resources