Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to connect to Google Cloud SQL from App Engine #103

Closed
anthonycastelli opened this issue Sep 9, 2018 · 7 comments
Closed

Ability to connect to Google Cloud SQL from App Engine #103

anthonycastelli opened this issue Sep 9, 2018 · 7 comments

Comments

@anthonycastelli
Copy link
Member

Google Cloud does some weird things when trying to connect to an SQL instance through App Engine. Connecting directly via an IP address is blocked from App Engine, so the way you have to connect is via a socket/proxy.

Here is a bit more info on the whole thing: https://cloud.google.com/sql/docs/postgres/sql-proxy

I've tried just about everything you can possibly think of.

Here is a bit more info regarding connecting to an SQL instance from App Engine.

https://cloud.google.com/sql/docs/postgres/connect-app-engine

@MrMage MrMage changed the title Ability to connect to Google Cloud SQL Ability to connect to Google Cloud SQL from App Engine Sep 10, 2018
@MrMage
Copy link
Contributor

MrMage commented Sep 10, 2018

I think it would be necessary to re-implement Google's client authentication code for Cloud SQL in Swift; here's a Java example.

Note that it is already possible to connect to Cloud SQL from Kubernetes Engine, however, through the Cloud SQL Proxy sidecar. I am successfully using that approach in production.

@anthonycastelli
Copy link
Member Author

anthonycastelli commented Sep 10, 2018

After doing some additional research and looking into the ruby implementation, the socket should be something like
/cloudsql/<project-id>:<sql-locaiton-id>:<sql-instance-name>

databases.add(database:
    PostgreSQLDatabase(config:
        PostgreSQLDatabaseConfig(
            hostname: Environment.get("POSTGRES_SOCKET_PATH") ?? Environment.get("POSTGRES_HOST") ?? "localhost",
            port: Int(Environment.get("POSTGRES_PORT") ?? "5432") ?? 5432,
            username: Environment.get("POSTGRES_USER") ?? "default",
            database: Environment.get("POSTGRES_DATABASE") ?? "default_database",
            password: Environment.get("POSTGRES_PASSWORD")
            )
        ),
    as: .psql)

My env variable POSTGRES_SOCKET_PATH has the socket path in the format above. According to what I have read, that is the socket that App Engine should connect to.

With the current implementation of Postgres' driver, that socket form wouldn't work, right?

@anthonycastelli
Copy link
Member Author

All that was needed to connect to the Postgres database was a unix socket, which I had originally thought, but I was missing the socket file at the end of the path. The full path should look like this
/cloudsql/project-id:location-id:instance-name/.s.PGSQL.5432

@MrMage
Copy link
Contributor

MrMage commented Sep 10, 2018

Wow, I didn’t know that. Does App Engine auto-mount a virtual file system in that location?

Anyway, glad to hear that adding Postgres support to the driver was useful.

@anthonycastelli
Copy link
Member Author

The virtual file system, I'm not sure of. I assume it has something mounted there for you to gain access to.

@christopherkarani
Copy link

Did you ever see a similar error when trying to use the unix socket like connect(descriptor:addr:size:) failed: No such file or directory (errno: 2)

@thecoolwinter
Copy link

Did you ever see a similar error when trying to use the unix socket like connect(descriptor:addr:size:) failed: No such file or directory (errno: 2)

This is the current error I'm having, has anyone had success connecting using unix sockets?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants