Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to a google cloud SQL from an app engine instance

I want to run an elixir application in google app engine flexible environment.

I also have a postgresql server running on google cloud SQL.

I am able to deploy the application inside the docker container. The problem is that I can't find how to configure the connection to the postgresql server.

As far as I understand the app engine instances run the cloud SQL proxy but I keep getting the following error inside the instance: [error] Postgrex.Protocol (#PID<0.232.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused

like image 948
Yonatan Naor Avatar asked May 13 '17 22:05

Yonatan Naor


People also ask

How do I connect to a GCP SQL instance?

In the Google Cloud console, go to the Cloud SQL Instances page. To open the Overview page of an instance, click the instance name. Select Connections from the SQL navigation menu. In the Authorized networks section, click Add network and enter the IP address of the machine where the client is installed.

Can App Engine Standard connect to on Prem database?

Because App Engine and Compute Engine use the same networking infrastructure, you can use the VPN connection to establish a connection between the App Engine app and your on-premises database using the database server's internal IP address.


1 Answers

i'm getting the same errors with a phoenix/elixir app that i'm trying to move out of container engine and into app engine flexible environment, just can't figure out what to use as the hostname.

in my php app in app engine i can just use :/cloudsql/projectname:region:dbname for the same database, but it doesn't seem to work that way for elixir

I also tried using the IP address of the cloud sql instance directly, but that wasn't working either.

in my app.yaml i have

env_variables:
  MIX_ENV: staging
  port: 8080
  DB_ENV_POSTGRES_HOST: :/cloudsql/projectname:region:dbame
  DB_ENV_POSTGRES_USER: userman
  DB_ENV_POSTGRES_PASSWORD: password

and in conf/staging.exs i have

config :guidebooks_api, GuidebooksApi.Repo,
  adapter: Ecto.Adapters.Postgres,
  username: System.get_env("DB_ENV_POSTGRES_USER") || "postgres",
  password: System.get_env("DB_ENV_POSTGRES_PASSWORD") || "postgres",
  hostname: System.get_env("DB_ENV_POSTGRES_HOST") || "localhost",
  database: "databasename",
  pool_size: 10,
  types: GuidebooksApi.PostgresTypes
like image 59
phlare Avatar answered Oct 05 '22 09:10

phlare