\connect
建立到 PostgreSQL 服务器的新连接。可以使用位置语法(数据库名称、用户、主机和端口中的一个或多个)指定要使用的连接参数,也可以使用 conninfo 连接字符串,详见 第 32.1.1 节 。如果没有给出参数,则使用与之前相同的参数建立新连接。
当前查看 PostgreSQL 18.6。
说明
建立到 PostgreSQL 服务器的新连接。可以使用位置语法(数据库名称、用户、主机和端口中的一个或多个)指定要使用的连接参数,也可以使用 conninfo 连接字符串,详见 第 32.1.1 节 。如果没有给出参数,则使用与之前相同的参数建立新连接。
- 客户端
- psql 18.6
- 区分大小写的写法
- \connect
用法
\c or \connect [ -reuse-previous= on|off ] [ dbname [ username ] [ host ] [ port ] | conninfo ]手册定义
\c或\connect [ -reuse-previous=on|off] [dbname[username] [host] [port] |conninfo]-
建立到PostgreSQL服务器的新连接。可以使用位置语法(数据库名称、用户、主机和端口中的一个或多个)指定要使用的连接参数,也可以使用
conninfo连接字符串,详见第 32.1.1 节。如果没有给出参数,则使用与之前相同的参数建立新连接。将
dbname、username、host或port中的任意一个指定为-,等同于省略该参数。新连接可以重用前一个连接的连接参数;不仅包括数据库名称、用户、主机和端口,还包括其他设置,如
sslmode。默认情况下,参数在位置语法中被重用,但在给定conninfo字符串时不会被重用。传递-reuse-previous=on或-reuse-previous=off作为第一个参数将覆盖该默认设置。如果参数被重用,则任何未明确指定为位置参数或在conninfo字符串中的参数将从现有连接的参数中获取。一个例外是,如果使用位置语法更改host设置,使其不同于先前的值,则现有连接参数中存在的任何hostaddr设置将被删除。此外,仅当用户、主机和端口设置未更改时,才会重用现有连接使用的任何密码。当命令既不指定也不重用特定参数时,将使用libpq的默认值。如果新连接成功建立,则关闭先前的连接。如果连接尝试失败(用户名错误,访问被拒绝等),且psql处于交互模式,则保留先前的连接。但在执行非交互脚本时,旧连接将被关闭并报告错误。这可能会或可能不会终止脚本;如果没有终止,直到成功执行另一个
\connect命令之前,所有访问数据库的命令都将失败。采用这种区别,一方面是为了方便用户应对输入笔误,另一方面是为了防止脚本意外操作错误的数据库。请注意,每当\connect命令尝试重新使用参数时,重新使用的值是最后一个成功连接的值,而不是随后的任何失败尝试的值。但是,在非交互\connect失败的情况下,不允许稍后重新使用参数,因为脚本可能期望从失败的\connect中重新使用值。示例:
=> \c mydb myuser host.dom 6432 => \c service=foo => \c "host=localhost port=5432 dbname=mydb connect_timeout=10 sslmode=disable" => \c -reuse-previous=on sslmode=require -- changes only sslmode => \c postgresql://tom@localhost/mydb?application_name=myapp
相关条目
文档与源码
来源构建
- 版本
- 18.6
- 构建
- https://ftp.postgresql.org/pub/source/v18.6/postgresql-18.6.tar.bz2
- 来源指纹
ee8d1a3612338fd9adf250730cb640fcc5233b5491337cc00a316a44e3a0b9f8
版本比较
PostgreSQL 16 → 17: 属性变化。
以下差异保留原始字段名与英文源描述。
--- PostgreSQL 16
+++ PostgreSQL 17
@@ -1,5 +1,5 @@
{
- "definition": "Establishes a new connection to a PostgreSQL server. The connection parameters to use can be specified either using a positional syntax (one or more of database name, user, host, and port), or using a conninfo connection string as detailed in Section 34.1.1 . If no arguments are given, a new connection is made using the same parameters as before. Specifying any of dbname , username , host or port as - is equivalent to omitting that parameter. The new connection can re-use connection parameters from the previous connection; not only database name, user, host, and port, but other settings such as sslmode . By default, parameters are re-used in the positional syntax, but not when a conninfo string is given. Passing a first argument of -reuse-previous=on or -reuse-previous=off overrides that default. If parameters are re-used, then any parameter not explicitly specified as a positional parameter or in the conninfo string is taken from the existing connection's parameters. An exception is that if the host setting is changed from its previous value using the positional syntax, any hostaddr setting present in the existing connection's parameters is dropped. Also, any password used for the existing connection will be re-used only if the user, host, and port settings are not changed. When the command neither specifies nor reuses a particular parameter, the libpq default is used. If the new connection is successfully made, the previous connection is closed. If the connection attempt fails (wrong user name, access denied, etc.), the previous connection will be kept if psql is in interactive mode. But when executing a non-interactive script, the old connection is closed and an error is reported. That may or may not terminate the script; if it does not, all database-accessing commands will fail until another \\connect command is successfully executed. This distinction was chosen as a user convenience against typos on the one hand, and a safety mechanism that scripts are not accidentally acting on the wrong database on the other hand. Note that whenever a \\connect command attempts to re-use parameters, the values re-used are those of the last successful connection, not of any failed attempts made subsequently. However, in the case of a non-interactive \\connect failure, no parameters are allowed to be re-used later, since the script would likely be expecting the values from the failed \\connect to be re-used. Examples: => \\c mydb myuser host.dom 6432 => \\c service=foo => \\c \"host=localhost port=5432 dbname=mydb connect_timeout=10 sslmode=disable\" => \\c -reuse-previous=on sslmode=require -- changes only sslmode => \\c postgresql://tom@localhost/mydb?application_name=myapp",
+ "definition": "Establishes a new connection to a PostgreSQL server. The connection parameters to use can be specified either using a positional syntax (one or more of database name, user, host, and port), or using a conninfo connection string as detailed in Section 32.1.1 . If no arguments are given, a new connection is made using the same parameters as before. Specifying any of dbname , username , host or port as - is equivalent to omitting that parameter. The new connection can re-use connection parameters from the previous connection; not only database name, user, host, and port, but other settings such as sslmode . By default, parameters are re-used in the positional syntax, but not when a conninfo string is given. Passing a first argument of -reuse-previous=on or -reuse-previous=off overrides that default. If parameters are re-used, then any parameter not explicitly specified as a positional parameter or in the conninfo string is taken from the existing connection's parameters. An exception is that if the host setting is changed from its previous value using the positional syntax, any hostaddr setting present in the existing connection's parameters is dropped. Also, any password used for the existing connection will be re-used only if the user, host, and port settings are not changed. When the command neither specifies nor reuses a particular parameter, the libpq default is used. If the new connection is successfully made, the previous connection is closed. If the connection attempt fails (wrong user name, access denied, etc.), the previous connection will be kept if psql is in interactive mode. But when executing a non-interactive script, the old connection is closed and an error is reported. That may or may not terminate the script; if it does not, all database-accessing commands will fail until another \\connect command is successfully executed. This distinction was chosen as a user convenience against typos on the one hand, and a safety mechanism that scripts are not accidentally acting on the wrong database on the other hand. Note that whenever a \\connect command attempts to re-use parameters, the values re-used are those of the last successful connection, not of any failed attempts made subsequently. However, in the case of a non-interactive \\connect failure, no parameters are allowed to be re-used later, since the script would likely be expecting the values from the failed \\connect to be re-used. Examples: => \\c mydb myuser host.dom 6432 => \\c service=foo => \\c \"host=localhost port=5432 dbname=mydb connect_timeout=10 sslmode=disable\" => \\c -reuse-previous=on sslmode=require -- changes only sslmode => \\c postgresql://tom@localhost/mydb?application_name=myapp",
"modifiers": [],
"signature": "\\c or \\connect [ -reuse-previous= on|off ] [ dbname [ username ] [ host ] [ port ] | conninfo ]",
"spellings": [
比较已记录的接口与属性,排除来源指纹和构建元数据。某个样本中没有记录,不能据此判断实际引入或移除的版本。
相关条目
导出 JSON · 返回psql 命令 · 收录范围为 PostgreSQL 10 至 20;最早采样版本不一定是实际引入版本。