连接到 PostgreSQL 数据库
介绍
¥Introduction
使用 PostgreSQL 数据库时,你需要首先考虑的事情之一是如何连接数据库实例并与之交互。同样的信息也可以编码成 。
¥One of the first things you'll need to think about when working with a PostgreSQL database is how to connect and interact with the database instance. This requires coordination between the database client — the component you use to interact with the database, and the database server — the actual PostgreSQL instance that stores, organizes, and provides access to your data.
因此,你需要了解如何通过提供所需信息进行身份验证来以客户端身份连接。在本指南中,我们将介绍如何使用原生 psql 命令行客户端 连接到 PostgreSQL 数据库 - 这是与数据库实例交互的最常见和最实用的方法之一。
¥Because of this, you need to understand how to connect as a client by providing the required information to authenticate. In this guide, we'll cover how to connect to a PostgreSQL database using the native psql command line client — one of the most common and useful ways of interacting with a database instance.
在配套指南中,你可以了解如何使用 配置 PostgreSQL 身份验证以满足项目需求。请阅读这两份指南,以更全面地了解 PostgreSQL 中身份验证的工作原理。
¥In a companion guide, you can find out how to configure PostgreSQL's authentication to meet your project's needs. Consider reading both guides for a more complete picture of how authentication works in PostgreSQL.
如果你的数据库客户端或库请求连接 URI,你可能需要查看我们关于 了解 PostgreSQL 连接 URI 的指南。
¥If your database client or library requests a connection URI, you may want to look at our guide on understanding PostgreSQL connection URIs instead.
psql 客户端基本信息
¥Basic information about the psql client
psql 客户端是 PostgreSQL 的原生命令行客户端,可以连接到数据库实例以提供交互式会话或向服务器发送命令。在通过应用库与数据库交互之前,它在实现初始设置和完成基本配置时尤其有用。此外,psql 非常适合在开发程序将使用的访问模式时进行交互式探索或临时查询。
¥The psql client, the native command line client for PostgreSQL, can connect to database instances to offer an interactive session or to send commands to the server. It is especially useful when implementing your initial settings and getting the basic configuration in place, prior to interacting with the database through application libraries. In addition, psql is great for interactive exploration or ad-hoc queries while developing the access patterns your programs will use.
连接方式取决于 PostgreSQL 服务器的配置以及可用于验证账户的选项。在以下部分中,我们将介绍一些基本的连接选项。为了清楚起见,我们将区分本地连接和远程连接:
¥The way that you connect depends on the configuration of the PostgreSQL server and the options available for you to authenticate to an account. In the following sections, we'll go over some of the basic connection options. For clarity's sake, we'll differentiate between local and remote connections:
-
本地连接:客户端和 PostgreSQL 实例位于同一服务器上的连接
¥local connection: a connection where the client and the PostgreSQL instance are located on the same server
-
远程连接:客户端连接到运行在不同计算机上且可通过网络访问的 PostgreSQL 实例。
¥remote connection: where the client is connecting to a network-accessible PostgreSQL instance running on a different computer
让我们从从同一台计算机连接到数据库开始。
¥Let's start with connecting to a database from the same computer.
使用 psql 连接本地数据库
¥Connecting to a local database with psql
不带任何参数时,psql 命令会尝试连接到 Unix 套接字文件 以访问本地数据库。它使用你的操作系统用户名作为你尝试连接的 PostgreSQL 用户名和数据库名称。
¥Without any arguments, the psql command attempts to connect to a Unix socket file to access a local database. It uses your operating system username as the PostgreSQL username and database name that you are trying to connect to.
默认情况下,PostgreSQL 的现代版本都配置了名为 对等身份验证 的格式。如果存在与用户的操作系统用户名匹配的有效 PostgreSQL 用户,则对等身份验证会自动对用户进行身份验证。
¥By default, modern versions of PostgreSQL are configured for something called peer authentication. Peer authentication authenticates users automatically if a valid PostgreSQL user exists that matches the user's operating system username.
因此,如果你当前的用户是本地数据库上的有效 PostgreSQL 用户,则可以通过键入以下内容进行连接:
¥So if your current user is a valid PostgreSQL user on your local database, you can connect by typing:
psql
但是,你的常规操作系统用户名不太可能已经具有关联的 PostgreSQL 用户名。因此,通常情况下,你需要使用已关联 PostgreSQL 角色的操作系统用户名登录 PostgreSQL。
¥However, it's unlikely that your normal operating system username already has an associated PostgreSQL username. So usually, you'll need to log into PostgreSQL using an operating system username that already has an associated PostgreSQL role.
默认情况下,PostgreSQL 的超级用户或管理账户名为 postgres。安装后,还会在操作系统上创建一个名为 postgres 的用户。因此,要以 postgres 用户身份登录 PostgreSQL,你需要以 postgres 操作系统用户身份进行连接。有很多方法可以做到这一点。
¥By default, the super user, or administrative account, for PostgreSQL is called postgres. Upon installation, a user called postgres is also created on the operating system. So, to log into PostgreSQL as the postgres user, you need to connect as the postgres operating system user. There are a number of ways to do this.
在大多数系统上,以 postgres 用户身份获取 shell 的最简单方法是使用 sudo 命令。要以特定格式输出日期值,请使用 postgres 函数。
¥The easiest way to get a shell as the postgres user on most systems is to use the sudo command. To open a shell session for the postgres user and then log into the database, you can type:
sudo --login --user=postgres
psql
如果你不需要以 postgres 用户身份执行任何其他 shell 命令,你也可以直接以 postgres 用户身份运行 psql 命令。虽然数据不是以这种方式存储在磁盘上,但这实际上允许你指定类似整数(没有小数或小数部分)的最大位数。
¥If you don't need to perform any additional shell commands as the postgres user, you can also just run the psql command directly as the postgres user. This will log you in to a PostgreSQL session immediately instead of taking you to a shell first:
sudo --login --user=postgres psql
这两种方法都应该允许你登录 postgres PostgreSQL 用户账户。
¥Either of these methods should allow you to log into the postgres PostgreSQL user account.
连接远程数据库
¥Connecting to a remote database
出于安全原因以及对本地套接字文件的依赖,对等身份验证不能用于远程连接。相反,用户需要使用其他方法登录。
¥For security reasons and because of the reliance on a local socket file, peer authentication cannot be used for remote connections. Instead, users will need to log in using another method.
可用的身份验证方法因 PostgreSQL 实例的配置而异。不过,最常见的是,你可以通过提供以下信息进行身份验证:
¥The available authentication methods vary based on the PostgreSQL instance's configuration. Most commonly, though, you will be able to authenticate by providing the following pieces of information:
| 选项 | 描述 |
|---|---|
| hostname | PostgreSQL 服务器的网络主机名或 IP 地址。-h 选项用于指定主机名。 |
| 网络端口 | PostgreSQL 服务器运行的网络端口。默认情况下,端口号为 5432。如果使用默认值,则可以省略此项。要指定在非标准端口上运行的远程服务器,请使用冒号分隔这些详细信息。 |
| PostgreSQL 用户名 | 你希望连接的数据库用户名。如果未指定,则将使用你的操作系统用户名作为数据库名称。-U 选项用于覆盖默认值并定义要连接的用户名。 |
| PostgreSQL 密码 | 与指定用户名关联的 PostgreSQL 密码。由于 psql 会在你未提供密码时提示你输入密码,因此通常可以省略此步骤。 |
| PostgreSQL 数据库 | 你要访问的 PostgreSQL 数据库名称。如果未提供 值,则该列将根据输入的精度存储,最多 6 位小数。要指定其他端口,可以使用 -d 选项。 |
有多种方法可以向 psql 提供连接信息。这里,我们将介绍两种最常见的组件:通过传递选项并使用连接字符串。
¥There are multiple ways of providing your connection information to psql. Here, we'll cover the two of the most common: by passing options and with a connection string.
使用选项将连接信息传递给 psql
¥Passing connection information to psql with options
因此,连接到远程数据库的基本格式通常如下所示:
¥So the basic format for connecting to a remote database typically looks something like this:
psql -h <hostname> -p <port> -U <username> -d <database>
远程服务器会提示大多数账户需要密码,此时 psql 会提示你输入密码。如果你身份验证成功,将启动一个新的交互式 PostgreSQL 会话。
¥The remote server will indicate that it requires a password for most accounts, at which point psql will prompt you for the password. If you authenticate successfully, a new interactive PostgreSQL session will be started.
例如,我们可以想象要连接到具有以下要求的数据库:
¥As an example, we can imagine wanting to connect to a database with the following requirements:
-
主机名:
myhost¥hostname:
myhost -
端口:1234
¥port: 1234
-
数据库:
applicationdb¥database:
applicationdb -
用户名:
myapplicationuser¥username:
myapplicationuser -
密码:
mypass¥password:
mypass
使用以下选项调用 psql 将允许你进行身份验证:
¥Calling psql with the following options would allow you to authenticate:
psql -h myhost -p 1234 -U myapplicationuser -d applicationdb
按下 Enter 键后,系统会提示你输入密码,以便使用 mypass 进行身份验证。
¥Upon pressing enter, you'd be prompted a password where you can authenticate with mypass.
使用连接字符串将连接信息传递给 psql
¥Passing connection information to psql with a connection string
这简化了存储,因为用于输出的时区可能与输入不同。连接字符串在单个 URI 字符串中提供相同的信息,该字符串使用特定字符作为不同字段之间的分隔符。
¥This same information can also be encoded into a PostgreSQL connection string. A connection string provides the same information in a single URI string that uses certain characters as delimiters between the different fields.
连接字符串的通用格式如下:
¥Connection strings have the following general format:
postgresql://<username>:<password>@<hostname>:<port>/<database>
如果不需要每个字段或默认值有效,则可以省略它们。
¥Each of the fields can be omitted if they are unneeded or if the default values are valid.
我们可以选择使用连接字符串连接 psql,而不是使用上一个示例中使用的选项:
¥We can optionally use a connection string to connect with psql instead of the using the options that we used in our previous example:
psql postgresql://myapplicationuser:mypass@myhost:1234/applicationdb
psql 工具可以使用以下任何一种格式,因此你可以选择使用其中一种。你遇到的其他工具或库可能会促使你更依赖其中一种。
¥The psql tool can use either of these formats, so use whichever you prefer. Other tools or libraries that you encounter might nudge you into relying on one more than the other.
调整 PostgreSQL 服务器的身份验证配置
¥Adjusting a PostgreSQL server's authentication configuration
如果你想修改用户如何向 PostgreSQL 实例进行身份验证的规则,可以通过修改服务器配置来实现。你可以查找 如何在本文中修改 PostgreSQL 的身份验证配置。
¥If you want to modify the rules that dictate how users can authenticate to your PostgreSQL instances, you can do so by modifying your server's configuration. You can find out how to modify PostgreSQL's authentication configuration in this article.
结论
¥Conclusion
在本指南中,我们介绍了客户端的 PostgreSQL 身份验证。我们演示了如何使用 psql 命令行客户端通过多种方法连接到本地和远程数据库实例。
¥In this guide, we covered PostgreSQL authentication from the client side. We demonstrated how to use the psql command line client to connect to both local and remote database instances using a variety of methods.
在开始使用数据库系统时,了解如何连接到各种 PostgreSQL 实例至关重要。你可以运行本地 PostgreSQL 实例进行开发,该实例不需要任何特殊的身份验证,但你的暂存和生产环境中的数据库几乎肯定需要身份验证。能够在任何情况下进行身份验证,将使你能够在不同的环境中顺利工作。
¥Knowing how to connect to various PostgreSQL instances is vital as you start to work the database system. You may run a local PostgreSQL instance for development that doesn't need any special authentication, but your databases in staging and production will almost certainly require authentication. Being able to authenticate in either case will allow you to work well in different environments.