user4216977
user4216977

Reputation:

Google App Engine Rails Postgres won't connect to Cloud SQL database: No such file or directory

I have a Rails app that should be hosted via Google Cloud App Engine. It has a Postgres database setup. I followed this tutorial: https://cloud.google.com/ruby/rails/using-cloudsql-postgres

I added all my configurations in app.yaml:

entrypoint: bundle exec rackup --port $PORT
env: flex
runtime: ruby

env_variables:
  SECRET_KEY_BASE: [SECRET_KEY]

beta_settings:
  cloud_sql_instances: [INSTANCE]

and database.yml:

production:
  adapter: postgresql
  encoding: unicode
  pool: 5
  timeout: 5000
  username: postgres
  password: [DB_PASSWORD]
  database: [DB_DATABASE]
  host: /cloudsql/[INSTANCE]

The Cloud SQL instance is setup correctly as I can connect to it using Cloud SQL proxy: https://cloud.google.com/sql/docs/postgres/connect-admin-proxy

After deployment to App Engine and trying to migrate it gives me the follwoing 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/[INSTANCE]/.s.PGSQL.5432"?

This also happens with a running proxy locally and trying to

RAILS_ENV=production rake db:create rake db:migrate

I see that /cloudsql folder is missing when I SSH into the App Engine's VM. I double-checked, access rights should be there and Cloud SQL API and Cloud SQL Admin API are enabled. I also noticed that when SSHing into the App Engine's VMs (apparently there are two, I tried both of them) and running ls the directories are completely empty. The deployment worked though as I can access the app but not the controller that require DB connection.

Upvotes: 0

Views: 922

Answers (1)

Jan Hernandez
Jan Hernandez

Reputation: 4620

I followed this old but better explained GCP tutorial(this tutorial is designed to use cloud shell) and I realized that is mandatory to have running Cloud SQL Proxy on your local env (in another terminal or background)

Cloud sql proxy will create the unix socket file

Listening on /cloudsql/tetsingfakeproject:us-central1:testdatabase ce/.s.PGSQL.5432 for tetsingfakeproject:us-central1:testdatabase

This in order to perform the migrations, and you can pass that step on the local environment.

Notes about using this tutorial

I followed all steps of the old tutorial with some alternative steps

In the step 3, I ran the newest command to enable the SQL admin api:

gcloud service enable sqladmin.googleapis.com

In the step 10, I installed the pg and appengine packages using these commands instead modifying the file "Gemfile"

bundle add pg
bundle add appengine

If the deploy time of this ruby app takes more than 10 minutes, please run this command

gcloud config set app/cloud_build_timeout 3600s

And run gcloud app deploy again.

following this tutorial I can get a functional App Engine Service using ruby + postgresql, I had a lot of issues using the other tutorial

Upvotes: 0

Related Questions