热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

PostgreSQL数据库网络层——libpq连接字符串和参数KV

ConnectionStrings几个libpq函数解析用户指定的字符串以获取连接参数。这些字符串有两种可接受的格式:普通关键字值字符串和URI。URI通常遵循RF

Connection Strings

几个 libpq 函数解析用户指定的字符串以获取连接参数。 这些字符串有两种可接受的格式:普通关键字/值字符串和 URI。 URI 通常遵循 RFC 3986,但允许多主机连接字符串,如下所述。
Several libpq functions parse a user-specified string to obtain connection parameters. There are two accepted formats for these strings: plain keyword/value strings and URIs. URIs generally follow RFC 3986, except that multi-host connection strings are allowed as further described below.

Keyword/Value Connection Strings
在关键字/值格式中,每个参数设置的形式为关键字 = 值,设置之间有空格。 设置等号周围的空格是可选的。 要写入空值或包含空格的值,请用单引号将其括起来,例如关键字 = ‘a value’。 值中的单引号和反斜杠必须用反斜杠转义,即 ’ 和 \。比如host=localhost port=5432 dbname=mydb connect_timeout=10
In the keyword/value format, each parameter setting is in the form keyword = value, with space(s) between settings. Spaces around a setting’s equal sign are optional. To write an empty value, or a value containing spaces, surround it with single quotes, for example keyword = ‘a value’. Single quotes and backslashes within a value must be escaped with a backslash, i.e., \' and \\.

Connection URIs
连接 URI 的一般形式是:
在这里插入图片描述
URI 方案指示符可以是 postgresql:// 或 postgres://。 其余的每个 URI 部分都是可选的。 以下示例说明了有效的 URI 语法:
The URI scheme designator can be either postgresql:// or postgres://. Each of the remaining URI parts is optional. The following examples illustrate valid URI syntax:
在这里插入图片描述
通常出现在 URI 的分层部分中的值也可以作为命名参数给出。 例如:postgresql:///mydb?host=localhost&port=5433

所有命名参数必须与第 34.1.2 节中列出的关键字匹配,除了为了与 JDBC 连接 URI 兼容,ssl=true 的实例被转换为 sslmode=require。
如果连接 URI 的任何部分包含具有特殊含义的符号,则需要使用百分比编码对其进行编码。 下面是一个示例,其中等号 (=) 替换为 %3D,空格字符替换为 %20:postgresql://user@localhost:5433/mydb?options=-c%20synchronous_commit%3Doff
All named parameters must match key words listed in Section 34.1.2, except that for compatibility with JDBC connection URIs, instances of ssl=true are translated into sslmode=require. The connection URI needs to be encoded with percent-encoding if it includes symbols with special meaning in any of its parts. Here is an example where the equal sign (=) is replaced with %3D and the space character with %20:postgresql://user@localhost:5433/mydb?options=-c%20synchronous_commit%3Doff
主机部分可以是主机名或 IP 地址。 要指定 IPv6 地址,请将其括在方括号中:postgresql://[2001:db8::1234]/database

主机部分被解释为参数主机的描述。 特别是,如果主机部分为空或看起来像绝对路径名,则选择 Unix 域套接字连接,否则将启动 TCP/IP 连接。 但是请注意,斜杠是 URI 分层部分中的保留字符。 因此,要指定一个非标准的 Unix 域套接字目录,要么省略 URI 的主机部分并将主机指定为命名参数,要么对 URI 的主机部分中的路径进行百分比编码:
The host part is interpreted as described for the parameter host. In particular, a Unix-domain socket connection is chosen if the host part is either empty or looks like an absolute path name, otherwise a TCP/IP connection is initiated. Note, however, that the slash is a reserved character in the hierarchical part of the URI. So, to specify a non-standard Unix-domain socket directory, either omit the host part of the URI and specify the host as a named parameter, or percent-encode the path in the host part of the URI:

postgresql:///dbname?host=/var/lib/postgresql
postgresql://%2Fvar%2Flib%2Fpostgresql/dbname

可以在单个 URI 中指定多个主机组件,每个组件都有一个可选的端口组件。 postgresql://host1:port1,host2:port2,host3:port3/ 形式的 URI 等效于 host=host1,host2,host3 port=port1,port2,port3 形式的连接字符串。 如下所述,将依次尝试每个主机,直到成功建立连接。It is possible to specify multiple host components, each with an optional port component, in a single URI. A URI of the form postgresql://host1:port1,host2:port2,host3:port3/ is equivalent to a connection string of the form host=host1,host2,host3 port=port1,port2,port3. As further described below, each host will be tried in turn until a connection is successfully established.

可以指定要连接的多个主机,以便按给定顺序尝试它们。在关键字/值格式中,host、hostaddr 和 port 选项接受逗号分隔的值列表。在指定的每个选项中必须给出相同数量的元素,例如,第一个 hostaddr 对应于第一个主机名,第二个 hostaddr 对应于第二个主机名,等等。作为一个例外,如果只指定一个端口,它适用于所有主机。It is possible to specify multiple hosts to connect to, so that they are tried in the given order. In the Keyword/Value format, the host, hostaddr, and port options accept comma-separated lists of values. The same number of elements must be given in each option that is specified, such that e.g., the first hostaddr corresponds to the first host name, the second hostaddr corresponds to the second host name, and so forth. As an exception, if only one port is specified, it applies to all the hosts.

在连接 URI 格式中,您可以在 URI 的主机组件中列出多个以逗号分隔的主机:端口对。In the connection URI format, you can list multiple host:port pairs separated by commas in the host component of the URI.

无论采用哪种格式,单个主机名都可以转换为多个网络地址。这方面的一个常见示例是同时具有 IPv4 和 IPv6 地址的主机。In either format, a single host name can translate to multiple network addresses. A common example of this is a host that has both an IPv4 and an IPv6 address.

当指定多个主机时,或者当一个主机名被翻译成多个地址时,所有的主机和地址都会依次尝试,直到一个成功。如果无法访问任何主机,则连接失败。如果成功建立连接,但身份验证失败,则不会尝试列表中的其余主机。When multiple hosts are specified, or when a single host name is translated to multiple addresses, all the hosts and addresses will be tried in order, until one succeeds. If none of the hosts can be reached, the connection fails. If a connection is established successfully, but authentication fails, the remaining hosts in the list are not tried.

如果使用密码文件,您可以为不同的主机设置不同的密码。对于列表中的每个主机,所有其他连接选项都是相同的;例如,不可能为不同的主机指定不同的用户名。If a password file is used, you can have different passwords for different hosts. All the other connection options are the same for every host in the list; it is not possible to e.g., specify different usernames for different hosts.


推荐阅读
  • Python SQLAlchemy库的使用方法详解
    本文详细介绍了Python中使用SQLAlchemy库的方法。首先对SQLAlchemy进行了简介,包括其定义、适用的数据库类型等。然后讨论了SQLAlchemy提供的两种主要使用模式,即SQL表达式语言和ORM。针对不同的需求,给出了选择哪种模式的建议。最后,介绍了连接数据库的方法,包括创建SQLAlchemy引擎和执行SQL语句的接口。 ... [详细]
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • Oracle Database 10g许可授予信息及高级功能详解
    本文介绍了Oracle Database 10g许可授予信息及其中的高级功能,包括数据库优化数据包、SQL访问指导、SQL优化指导、SQL优化集和重组对象。同时提供了详细说明,指导用户在Oracle Database 10g中如何使用这些功能。 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • 这篇文章主要介绍了Python拼接字符串的七种方式,包括使用%、format()、join()、f-string等方法。每种方法都有其特点和限制,通过本文的介绍可以帮助读者更好地理解和运用字符串拼接的技巧。 ... [详细]
  • PDO MySQL
    PDOMySQL如果文章有成千上万篇,该怎样保存?数据保存有多种方式,比如单机文件、单机数据库(SQLite)、网络数据库(MySQL、MariaDB)等等。根据项目来选择,做We ... [详细]
  • 本文介绍了在序列化时如何对SnakeYaml应用格式化,包括通过设置类和DumpSettings来实现定制输出的方法。作者提供了一个示例,展示了期望的yaml生成格式,并解释了如何使用SnakeYaml的特定设置器来实现这个目标。对于正在使用SnakeYaml进行序列化的开发者来说,本文提供了一些有用的参考和指导。摘要长度为169字。 ... [详细]
  • 本文介绍了在MacOS系统上安装MySQL的步骤,并详细说明了如何设置MySQL服务的开机启动和如何修改MySQL的密码。通过下载MySQL的macos版本并按照提示一步一步安装,在系统偏好设置中可以找到MySQL的图标进行设置。同时,还介绍了通过终端命令来修改MySQL的密码的具体操作步骤。 ... [详细]
  • 本文介绍了在MFC下利用C++和MFC的特性动态创建窗口的方法,包括继承现有的MFC类并加以改造、插入工具栏和状态栏对象的声明等。同时还提到了窗口销毁的处理方法。本文详细介绍了实现方法并给出了相关注意事项。 ... [详细]
  • 如何在php文件中添加图片?
    本文详细解答了如何在php文件中添加图片的问题,包括插入图片的代码、使用PHPword在载入模板中插入图片的方法,以及使用gd库生成不同类型的图像文件的示例。同时还介绍了如何生成一个正方形文件的步骤。希望对大家有所帮助。 ... [详细]
  • Postgresql备份和恢复的方法及命令行操作步骤
    本文介绍了使用Postgresql进行备份和恢复的方法及命令行操作步骤。通过使用pg_dump命令进行备份,pg_restore命令进行恢复,并设置-h localhost选项,可以完成数据的备份和恢复操作。此外,本文还提供了参考链接以获取更多详细信息。 ... [详细]
author-avatar
隔壁老吴
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有