使用SQLAlchemy通过Unix套接字连接到数据库 [英] Connect to a database over a unix socket using SQLAlchemy

查看:107
本文介绍了使用SQLAlchemy通过Unix套接字连接到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的云函数使用SQLAlchemy连接到我的Cloud SQL DB,但是我似乎无法计算出正确的连接字符串。

I'm trying to connect to my Cloud SQL DB using SQLAlchemy from my cloud function but I can't seem to work out the correct connection string.

DATABASE_URL=postgres://$DB_USER:$_DB_PWD@/$DB_NAME?unix_socket=/cloudsql/$DB_INSTANCE

哪个给了我错误:

pyscopg2.ProgrammingError: invalid dns: invalid connection option "unix_socket"

使用<<>通过unix套接字连接到Postgresql 9.6 DB的正确方法是什么? code> pyscopg2 ?

推荐答案

此处所需的特殊关键字是主机

The special keyword needed here is host:

DATABASE_URL=postgres://user:password@/dbname?host=/path/to/db

请注意,主机中的路径应该是路径,而不是套接字文件本身(psycopg2假定套接字具有标准命名约定 .s.PGSQL.5432

Note that the path in host should be a path, not the socket file itself (psycopg2 assumes the socket has the standard naming convention .s.PGSQL.5432)

https://docs.sqlalchemy.org/en/latest/dialects/postgresql.html#unix -domain-connections

这篇关于使用SQLAlchemy通过Unix套接字连接到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆