连接PostgreSQL时,如何指定参数

    xiaoxiao2025-12-28  6

    标签

    PostgreSQL , conninfo


    背景

    连接数据库时,如何配置默认参数?

    libpq

    https://www.postgresql.org/docs/9.6/static/libpq-connect.html#LIBPQ-CONNSTRING

    所有支持的连接参数都在以上链接中

    KV模式

    配置连接串时,可以使用KV模式,例子

    host=localhost port=5432 dbname=mydb connect_timeout=10

    URI模式

    也可以使用URI模式,例子

    postgresql:// postgresql://localhost postgresql://localhost:5433 postgresql://localhost/mydb postgresql://user@localhost postgresql://user:secret@localhost postgresql://other@localhost/otherdb?connect_timeout=10&application_name=myapp

    options

    对于其他参数,需要通过options指定,例子如下

    psql "host=localhost port=1921 dbname=postgres connect_timeout=10 sslmode=disable options='-c work_mem=64 -c lock_timeout=1'"

    参考

    https://www.postgresql.org/docs/9.6/static/libpq-connect.html#LIBPQ-CONNSTRING

    相关资源:python入门教程(PDF版)
    最新回复(0)