Skip to content

Support for Cloud SQL host within the dsn #419

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

Closed
gvbgduh opened this issue Mar 19, 2019 · 14 comments
Closed

Support for Cloud SQL host within the dsn #419

gvbgduh opened this issue Mar 19, 2019 · 14 comments

Comments

@gvbgduh
Copy link

gvbgduh commented Mar 19, 2019

Hi,

I'm trying to use asyncpg to connect to Google Cloud SQL together with [databases](https://github.com/encode/databases/), alembic and sometimes sqlalchemy. It's quite convenient to have one database url that can be used in different cases.

But there's one problem as the Cloud SQL host is in the format /cloudsql/<project>:<region>:<db_instance>.

Unfortunately, it breaks the parsing around https://github.com/MagicStack/asyncpg/blob/master/asyncpg/connect_utils.py#L182, so the port and host cannot be distinguished unless host is explicitly passed as kwarg.

Would it be possible to consider adding the support for such a case?

Naive solution seems to be to use rpartition there, so the url
'postgresql://USER:PWD@/DB?host=/cloudsql/::<db_instance>:5432'
can be used, but I can investigate this one more thorough if seems like a reasonable case.

@elprans
Copy link
Member

elprans commented Apr 3, 2019

Can you show the URL you are using with sqlalchemy?

@gvbgduh
Copy link
Author

gvbgduh commented Apr 7, 2019

The url is

f'postgresql://user:password@/db_name?host=/cloudsql/project-name:region:instance-name&port=5432'

so host is

/cloudsql/project-name:region:instance-name

but I have to make a workaround to pass host additionally as a kwarg, so asyncpg.connection. connect receives dsn and host as above as host from the kwargs takes precedence.

@gvbgduh
Copy link
Author

gvbgduh commented Apr 7, 2019

It could be either

f'postgresql://user:password@/db_name?host=/cloudsql/project-name:region:instance-name:5432'

the result is similar.
but it's parsed as

host = "/cloudsql/project-name"
port = "region:instance-name:5432"

@leosussan
Copy link

Bumping this - am having the exact same issue.

@ioistired
Copy link
Contributor

Try URL encoding the colons?

@Lawouach
Copy link
Contributor

Lawouach commented Aug 5, 2019

Hi there,

I'm trying to figure the state of this issue and it's not clear to me. Are we able to pass the CloudSQL /cloudsql/<project>:<region>:<db_instance> format? I tried to urlencode but I'm getting the same issue that actual string is not kep as it should.

@Lawouach
Copy link
Contributor

Lawouach commented Aug 6, 2019

Looking through the code a bit more. It seems the logic is to assume Host:Port from the get go (and call _parse_hostlist). Maybe a check before calling that function for the host starting with a /? Or change its name and make the check from within?

Lawouach added a commit to Lawouach/asyncpg that referenced this issue Aug 6, 2019
Contributes to MagicStack#419

Signed-off-by: Sylvain Hellegouarch <[email protected]>
Lawouach added a commit to Lawouach/asyncpg that referenced this issue Aug 6, 2019
Contributes to MagicStack#419

Signed-off-by: Sylvain Hellegouarch <[email protected]>
Lawouach added a commit to Lawouach/asyncpg that referenced this issue Aug 20, 2019
Contributes to MagicStack#419

Signed-off-by: Sylvain Hellegouarch <[email protected]>
elprans pushed a commit that referenced this issue Oct 3, 2019
Contributes to #419

Signed-off-by: Sylvain Hellegouarch <[email protected]>
@maxzheng
Copy link

This is working now in the latest version (with #470), maybe someone can close this ticket to avoid confusion?

@gvbgduh
Copy link
Author

gvbgduh commented Apr 11, 2020

awesome news @maxzheng!

@gvbgduh gvbgduh closed this as completed Apr 11, 2020
@JeffryCA
Copy link

JeffryCA commented Nov 5, 2021

Hi @maxzheng Can you tell me in detail how you made it work? I have been trying:

postrgres://user:passw@/cloudsql/projectId:region:instance-name/postgres (dbname)

@david-dest01
Copy link

Hi @maxzheng Can you tell me in detail how you made it work? I have been trying:

postrgres://user:passw@/cloudsql/projectId:region:instance-name/postgres (dbname)

Anyone actually answer this for you? I'm not able to get this going and there doesn't seem to be a clear example/overview of expected format.

@geminixiang
Copy link

Cloud run can't connect to Cloud SQL, have someone has correct url format for Cloud SQL

postgresql+asyncpg://username:password@/mydb?host=/cloudsql/project:asia-east1:instance

@geminixiang
Copy link

Cloud run can't connect to Cloud SQL, have someone has correct url format for Cloud SQL

postgresql+asyncpg://username:password@/mydb?host=/cloudsql/project:asia-east1:instance

this is correct url format

postgresql+asyncpg://username:password@/mydb?host=/cloudsql/project:asia-east1:instance

and cloud run must be configured to specify which instance you wanna connect to

gcloud beta run deploy $SERVICE \
--set-cloudsql-instances="$INSTANCE_CONNECTION_NAME"  # Remember to set

@ofer-pd
Copy link

ofer-pd commented Oct 11, 2024

FYI - When I tried setting the scheme to "postgresql+asyncpg" as recommended in the previous comment, I got the following error:

asyncpg.exceptions._base.ClientConfigurationError: invalid DSN: scheme is expected to be either "postgresql" or "postgres", got 'postgresql+asyncpg'

Using asyncpg v0.29.0.

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

10 participants