1
votes

I have been using GCP for a few days now but I am struggling to get my Ruby on Rails app to connect to Postgres hosted on Cloud SQL.

I have managed to connect locally via the cloud SQL proxy and execute migrations but I have not gotten past that.

Here is my database.yml production settings.

production:
  <<: *default
  adapter: postgresql
  database: databasename
  username: databaseuser
  password: databasepassword
  host: /cloudsql/project-name-172409:us-central1:application-name

Here is my app.yaml settings.

runtime: custom
env: flex

health_check:
  enable_health_check: false

beta_settings:
  cloud_sql_instances: project-name-172409:us-central1:application-name

env_variables:
  SECRET_KEY_BASE: 121212

My custom docker file inherits from the base ruby build and executes migrations.

The error i get is this.

PG::ConnectionBad: could not connect to server: No such file or directory
    Is the server running locally and accepting
2

2 Answers

2
votes

I had the same issue. I forgot to activate the Cloud SQL API. After activating it, everything worked like a charm. After activating I had to deploy everything again.

0
votes

It looks like CloudSQL is not mounted during the build of the docker image which makes sense. I was assuming it was never mounted because none of my builds finished due to the migrations failed which left me to presume that the database was not connecting.

Once I removed the migration execution code from my Dockerfile and build the Docker image, I SSH'd into the app engine instance to inspect if /cloudsql directory was there which it was. I am currently assuming that Rails can connect to the database as I am not getting any errors in my webapp. I will report back once I have confirmed rails is connecting to Postgres Cloud SQL.