【问题标题】:Connect to a database over a unix socket using SQLAlchemy使用 SQLAlchemy 通过 unix 套接字连接到数据库
【发布时间】:2019-07-24 20:17:54
【问题描述】:

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

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

这给了我错误:

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

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

【问题讨论】:

    标签: postgresql sqlalchemy google-cloud-functions google-cloud-sql unix-socket


    【解决方案1】:

    这里需要的特殊关键字是host

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

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

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

    【讨论】:

    • 无论出于何种原因,这对我不起作用,我也面临着 OP 的问题。
    • 对我有用,确保不要在连接字符串的末尾添加“.s.PGSQL.5432”,只添加它所在的目录
    • 我认为您目前(2021 年)需要 postgresql 而不是 postgres
    • 即使链接的文档也没有明确说明将端口放在哪里,用户“fred”通过“/tmp/big_db_sockets/.s.PGSQL”中的套接字连接到 db“big_db”的试验和错误.1234" 似乎是:postgresql://fred@:1234/big_db?host=/tmp/big_db_sockets
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-11
    • 2020-04-05
    相关资源
    最近更新 更多