设置本地 PostgreSQL 数据库
本页介绍如何安装和配置 PostgreSQL 数据库服务器 和 psql 命令行客户端。本指南将介绍如何在你的计算机上安装和设置这些组件以实现本地访问。
¥This page explains how to install and configure a PostgreSQL database server and the psql command line client. This guide will cover how to install and set up these components on your computer for local access.
本指南将涵盖以下平台:
¥This guide will cover the following platforms:
导航到与你将使用的平台相匹配的部分。
¥Navigate to the sections that match the platforms you will be working with.
在 Windows 上设置 PostgreSQL
¥Setting up PostgreSQL on Windows
PostgreSQL 项目提供了一个原生的 Windows 安装程序来安装和配置你的数据库。
¥The PostgreSQL project provides a native Windows installer to install and configure your database.
访问 PostgreSQL Windows 安装页面 查找安装程序链接。在页面开头,单击“下载安装程序”:
¥Visit the PostgreSQL Windows installation page to find a link to the installer. Click Download the installer at the start of the page:

在接下来的页面上,在 Windows x86-64 或 Windows x86-32 列中(取决于你计算机的架构),选择你要安装的 PostgreSQL 版本:
¥On the page that follows, in the Windows x86-64 or Windows x86-32 columns (depending on your computer's architecture), choose the PostgreSQL version you'd like to install:

在你选择的版本上单击“下载”,然后将文件保存到方便的位置。
¥Click Download on your chosen version and save the file to a convenient location.
下载完成后,双击文件运行安装程序(你可能需要确认是否允许该程序更改你的计算机):
¥Once the download completes, double click on the file to run the installer (you may have to confirm that you wish to allow the program to make changes to your computer):
在初始页面上,单击“下一步”开始配置安装。
¥Click Next on the initial page to begin configuring your installation.
在下一页中,选择你的安装目录:
¥On the next page, choose your installation directory:
单击“下一步”接受默认位置。
¥Click Next to accept the default location.
下一页允许你选择要安装的组件。你至少需要选择 PostgreSQL 服务器和命令行工具:
¥The next page allows you to choose which components you wish to install. You need the PostgreSQL Server and Command Line Tools selected at a minimum:
点击“下一步”继续。
¥Click Next to continue.
现在,选择数据库数据文件的存储位置:
¥Now, choose the location where the database data files will be stored:
单击“下一步”接受默认位置。
¥Click Next to accept the default location.
接下来,选择并确认 PostgreSQL 超级用户(称为 postgres)的管理密码:
¥Next, select and confirm an administrative password for the PostgreSQL superuser (called postgres):
完成后,单击“下一步”。
¥Click Next when you are finished.
选择服务器将监听的端口:
¥Choose the port that the server will listen to:
单击“下一步”接受默认的 5432 端口。
¥Click Next to accept the default 5432 port.
现在,选择数据库将使用的语言环境:
¥Now, pick the locale that your database will use:
单击“下一步”使用你计算机的默认语言环境。
¥Click Next to use the default locale of your computer.
安装的配置部分现已完成。你可以查看你所做选择的摘要:
¥The configuration portion of the installation is now complete. You can review a summary of the choices you've made:

如果一切正常,请点击“下一步”。
¥If everything looks correct, click Next.
最后,PostgreSQL 即可安装:
¥Finally, PostgreSQL is ready to be installed:
单击“下一步”开始安装过程。
¥Click Next to begin the installation process.
安装完成后,你可以使用 psql 命令行工具验证安装。
¥Once the installation is complete, you can verify the installation using the psql command line tool.
在开始菜单中,输入 psql 并点击该工具即可启动程序。系统将提示你输入要使用的连接详细信息。
¥In your start menu, type psql and click on the tool to launch the program. You will be prompted to enter the connection details that you wish to use.
按 Enter 键接受方括号中给出的默认选项。最终提示将要求输入你在设置期间配置的 postgres 用户的密码。
¥Press Enter to accept the default choices given in the square brackets. The final prompt will be for the password for the postgres user that you configured during setup.
成功验证后,你将进入与数据库的交互式 psql 会话。
¥Upon successfully authenticating, you will be dropped into an interactive psql session with your database.
完成后,输入以下命令退出会话:
¥When you are finished, exit the session by typing:
\quit
在 macOS 上设置 PostgreSQL
¥Setting up PostgreSQL on macOS
PostgreSQL 项目提供了一个原生的 macOS 安装程序来安装和配置你的数据库。
¥The PostgreSQL project provides a native macOS installer to install and configure your database.
访问 PostgreSQL macOS 安装页面 查找安装程序链接。在 EnterpriseDB 交互式安装程序部分的开头,单击“下载安装程序”:
¥Visit the PostgreSQL macOS installation page to find a link to the installer. Click Download the installer at the start of the Interactive Installer by EnterpriseDB section:

在接下来的页面上,在 Mac OS X 列中,选择你要安装的 PostgreSQL 版本:
¥On the page that follows, in the Mac OS X column, choose the PostgreSQL version you'd like to install:

在你选择的版本上单击“下载”,然后将文件保存到方便的位置。
¥Click Download on your chosen version and save the file to a convenient location.
下载完成后,在“下载”文件夹中找到 PostgreSQL 安装程序 DMG。双击下载的 DMG 文件以安装安装程序存档:
¥Once the download completes, find the PostgreSQL installer DMG in the Downloads folder. Double click on the downloaded DMG file to mount the installer archive:

在已安装的存档中,点击 PostgreSQL 安装程序包(系统可能会提示你确认是否允许该包更改你的计算机):
¥In the mounted archive, click on the PostgreSQL installer package (you may be prompted to confirm that you want the package to be able to make changes to your computer):

PostgreSQL 安装程序将打开欢迎屏幕:
¥The PostgreSQL installer will open to the welcome screen:

在初始页面上,单击“下一步”开始配置安装。
¥Click Next on the initial page to begin configuring your installation.
在下一页中,选择你的安装目录:
¥On the next page, choose your installation directory:

单击“下一步”接受默认位置。
¥Click Next to accept the default location.
下一页允许你选择要安装的组件。你至少需要选择 PostgreSQL 服务器和命令行工具:
¥The next page allows you to choose which components you wish to install. You need the PostgreSQL Server and Command Line Tools selected at a minimum:

点击“下一步”继续。
¥Click Next to continue.
现在,选择数据库数据文件的存储位置:
¥Now, choose the location where the database data files will be stored:

单击“下一步”接受默认位置。
¥Click Next to accept the default location.
在下一页中,选择并确认 PostgreSQL 超级用户(名为 postgres)的管理密码:
¥On the next page, select and confirm an administrative password for the PostgreSQL superuser (called postgres):

完成后,单击“下一步”。
¥Click Next when you are finished.
以下页面允许你选择服务器将监听的端口:
¥The following page lets you choose the port that the server will listen to:
单击“下一步”接受默认的 5432 端口。
¥Click Next to accept the default 5432 port.
现在,选择数据库将使用的语言环境:
¥Now, pick the locale that your database will use:

单击“下一步”使用你计算机的默认语言环境。
¥Click Next to use the default locale of your computer.
安装的配置部分现已完成。你可以查看你所做选择的摘要:
¥The configuration portion of the installation is now complete. You can review a summary of the choices you've made:

如果一切正常,请点击“下一步”。
¥If everything looks correct, click Next.
PostgreSQL 已准备好安装:
¥PostgreSQL is ready to be installed:

单击“下一步”开始安装过程。
¥Click Next to begin the installation process.
安装程序将在过程完成后确认完成:
¥The installer will confirm completion when the process completes:

现在 PostgreSQL 已安装完毕,你可以使用 psql 命令行工具验证安装。安装此客户端后,我们需要修改终端的 PATH 变量以便轻松访问它。
¥Now that PostgreSQL is installed, you can verify the installation using the psql command line tool. While this client is installed, we need to modify our terminal's PATH variable to access it easily.
打开一个新的终端窗口开始。首先,输入以下命令找到 PostgreSQL bin 目录:
¥Open a new terminal window to begin. First, find the PostgreSQL bin directory by typing:
ls -d /Library/PostgreSQL/*/bin
响应将是你的 PostgreSQL bin 目录的目录。例如:
¥The response will be the directory of your PostgreSQL bin directory. For example:
/Library/PostgreSQL/12/bin
现在你知道要使用的目录,请输入以下命令编辑 /etc/paths 文件:
¥Now that you know the directory to use, edit the /etc/paths file by typing:
sudo nano /etc/paths
系统将提示你输入计算机的管理密码才能继续:
¥You will be prompted for your computer's administrative password to continue:
将你找到的 bin 目录的路径添加到文件底部:
¥Add the path to bin directory that you found to the bottom of the file:

完成后,输入 CTL-X、Y,然后点击 ENTER 保存并关闭文件。
¥When you are finished, save and close the file by typing CTL-X, Y, and hitting ENTER.
要使用新的 PATH 设置,请打开一个新的终端窗口。在新窗口中,输入:
¥To use the new PATH settings, open a new terminal window. In the new window, type:
psql -U postgres
系统将提示你输入在 PostgreSQL 配置过程中设置的 postgres 用户的管理密码:
¥You will be prompted for the postgres user's administrative password that you set during the PostgreSQL configuration process:

成功验证后,你将进入与数据库的交互式 psql 会话。
¥Upon successfully authenticating, you will be dropped into an interactive psql session with your database.
要在 PostgreSQL 中提取日期的一部分,你可以使用 函数。
¥To exit the session when you are finished, type:
\quit
在 Linux 上设置 PostgreSQL
¥Setting up PostgreSQL on Linux
安装方法因你使用的 Linux 发行版而异。请按照以下与你的 Linux 发行版匹配的部分进行操作。
¥Installation methods differ depending on the Linux distribution you are using. Follow the section below that matches your Linux distribution.
Debian 和 Ubuntu
¥Debian and Ubuntu
你可以选择使用发行版默认存储库中提供的 PostgreSQL 版本,也可以使用 PostgreSQL 项目提供的存储库。默认仓库中的软件包已经过测试,可以与你的发行版提供的所有其他软件兼容,但版本可能较旧。PostgreSQL 项目中的软件包会更新,但可能需要额外配置。
¥You can either choose to use the version of PostgreSQL available in your distribution's default repositories or use repositories provided by the PostgreSQL project. Packages in the default repository are tested to work with all other software provided for your distribution, but may be older. Packages from the PostgreSQL project will be more up-to-date but may require extra configuration.
-
使用 PostgreSQL 项目的 Debian 和 Ubuntu 仓库安装
¥Install using the PostgreSQL project's Debian and Ubuntu repositories
使用 Debian 或 Ubuntu 默认仓库安装
¥Install using Debian or Ubuntu's default repositories
Ubuntu 和 Debian 都在其默认存储库中以软件包的形式提供了 PostgreSQL 服务器版本。PostgreSQL 版本可能比 PostgreSQL 网站上的版本旧,但这是在这些发行版上安装的最简单方法。
¥Both Ubuntu and Debian provide versions of PostgreSQL server as packages within their default repositories. The PostgreSQL version may be older than those found on the PostgreSQL website, but this is the simplest way to install on these distributions.
要安装 PostgreSQL 服务器,请使用发行版的包管理器安装 包:之后,安装 postgresql 包:
¥To install PostgreSQL server, update your computer's local package cache with the latest set of packages. Afterwards, install the postgresql package:
sudo apt update
sudo apt install postgresql
默认情况下,PostgreSQL 配置为使用 对等身份验证,如果用户的操作系统用户名与 PostgreSQL 内部名称匹配,则允许用户登录。
¥By default, PostgreSQL is configured to use peer authentication, which allows users to log in if their operating system user name matches a PostgreSQL internal name.
安装过程创建了一个名为 postgres 的操作系统用户,以匹配 postgres 数据库管理账户。要为 psql 用户打开 shell 会话并登录数据库,你可以输入:
¥The installation process created an operating system user called postgres to match the postgres database administrative account. To log into PostgreSQL with the psql client, use sudo to run the command as the postgres user:
sudo -u postgres psql
完成后,你可以输入以下命令退出 psql 会话:
¥When you are finished, you can exit the psql session by typing:
\quit
使用 PostgreSQL 项目的 Debian 和 Ubuntu 仓库安装
¥Install using the PostgreSQL project's Debian and Ubuntu repositories
如果你需要更新版本的 PostgreSQL,你可以使用 PostgreSQL 项目维护的仓库,而不是 Linux 发行版提供的仓库。
¥If you need a more up-to-date version of PostgreSQL, you can use the repositories maintained by the PostgreSQL project instead of those provided by your Linux distribution.
首先,输入以下命令将新的存储库定义添加到你的系统:
¥First, add a new repository definition to your system by typing:
echo "deb http://apt.postgresql.org/pub/repos/apt/ $(. /etc/os-release; echo $VERSION_CODENAME)-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list
接下来,将 PostgreSQL 软件包签名密钥添加到 apt,以便它信任新存储库中的软件包:
¥Next, add the PostgreSQL package signing key to apt so that it trusts the packages in the new repository:
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
设置好存储库后,你可以更新本地包索引,然后输入以下命令安装 PostgreSQL:
¥With the repository set up, you can update your local package index and then install PostgreSQL by typing:
sudo apt update
sudo apt install postgresql
默认情况下,PostgreSQL 配置为使用 对等身份验证,如果用户的操作系统用户名与 PostgreSQL 内部名称匹配,则允许用户登录。
¥By default, PostgreSQL is configured to use peer authentication, which allows users to log in if their operating system user name matches a PostgreSQL internal name.
安装过程创建了一个名为 postgres 的操作系统用户,以匹配 postgres 数据库管理账户。要为 psql 用户打开 shell 会话并登录数据库,你可以输入:
¥The installation process created an operating system user called postgres to match the postgres database administrative account. To log into PostgreSQL with the psql client, use sudo to run the command as the postgres user:
sudo -u postgres psql
完成后,你可以输入以下命令退出 psql 会话:
¥When you are finished, you can exit the psql session by typing:
\quit
CentOS 和 Fedora
¥CentOS and Fedora
你可以选择使用发行版默认存储库中提供的 PostgreSQL 版本,也可以使用 PostgreSQL 项目提供的存储库。默认仓库中的软件包已经过测试,可以与你的发行版提供的所有其他软件兼容,但版本可能较旧。PostgreSQL 项目中的软件包会更新,但可能需要额外配置。
¥You can either choose to use the version of PostgreSQL available in your distribution's default repositories or use repositories provided by the PostgreSQL project. Packages in the default repository are tested to work with all other software provided for your distribution, but may be older. Packages from the PostgreSQL project will be more up-to-date but may require extra configuration.
-
使用 PostgreSQL 项目的 CentOS 和 Fedora 仓库安装
¥Install using the PostgreSQL project's CentOS and Fedora repositories
使用 CentOS 或 Fedora 默认仓库安装
¥Install using CentOS or Fedora's default repositories
CentOS 和 Fedora 都在其默认存储库中以软件包的形式提供了 PostgreSQL 服务器版本。PostgreSQL 版本可能比 PostgreSQL 网站上的版本旧,但这是在这些发行版上安装的最简单方法。
¥Both CentOS and Fedora provide versions of PostgreSQL server as packages within their default repositories. The PostgreSQL version may be older than those found on the PostgreSQL website, but this is the simplest way to install on these distributions.
要使用 mysql-server 客户端登录 PostgreSQL,请使用 以 用户身份运行以下命令:
¥To install PostgreSQL server, use your distribution's package manager to install the mysql-server package:
对于 CentOS,请使用 yum 包管理器:
¥For CentOS use the yum package manager:
sudo yum install postgresql-server
对于 Fedora,请使用 dnf 包管理器:
¥For Fedora use the dnf package manager:
sudo dnf install postgresql-server
安装 MySQL 软件包后,请输入以下命令初始化数据库:
¥Once the MySQL package is installed, initialize the database by typing:
sudo postgresql-setup initdb
之后,输入以下命令启动服务:
¥Afterwards, start up the service by typing:
sudo systemctl start postgresql.service
你也可以选择通过输入以下命令在系统启动时自动启动 PostgreSQL:
¥Optionally, you can automatically start PostgreSQL on boot by typing:
sudo systemctl enable postgresql.service
默认情况下,PostgreSQL 配置为使用 对等身份验证,如果用户的操作系统用户名与 PostgreSQL 内部名称匹配,则允许用户登录。
¥By default, PostgreSQL is configured to use peer authentication, which allows users to log in if their operating system user name matches a PostgreSQL internal name.
安装过程创建了一个名为 postgres 的操作系统用户,以匹配 postgres 数据库管理账户。要为 psql 用户打开 shell 会话并登录数据库,你可以输入:
¥The installation process created an operating system user called postgres to match the postgres database administrative account. To log into PostgreSQL with the psql client, use sudo to run the command as the postgres user:
sudo -u postgres psql
完成后,你可以输入以下命令退出 psql 会话:
¥When you are finished, you can exit the psql session by typing:
\quit
使用 PostgreSQL 项目的 CentOS 和 Fedora 仓库安装
¥Install using the PostgreSQL project's CentOS and Fedora repositories
如果你需要更新版本的 PostgreSQL,你可以使用 PostgreSQL 项目维护的仓库,而不是 Linux 发行版提供的仓库。
¥If you need a more up-to-date version of PostgreSQL, you can use the repositories maintained by the PostgreSQL project instead of those provided by your Linux distribution.
要连接到托管在 上监听的 PostgreSQL 服务器上名为 的数据库,你可以输入:
¥To configure the PostgreSQL project's repository, download and install the repository setup package with in your terminal.
对于 CentOS,请执行以下命令:
¥For CentOS execute the following command:
sudo yum install "https://download.postgresql.org/pub/repos/yum/reporpms/EL-$(. /etc/os-release; echo $VERSION_ID)-x86_64/pgdg-redhat-repo-latest.noarch.rpm"
对于 Fedora,请使用以下命令:
¥For Fedora use this command:
sudo dnf install "https://download.postgresql.org/pub/repos/yum/reporpms/F-$(. /etc/os-release; echo $VERSION_ID)-x86_64/pgdg-fedora-repo-latest.noarch.rpm"
如果你使用的是 CentOS 8,则还必须禁用系统的 PostgreSQL 模块,以防止它干扰存储库的 PostgreSQL 版本。为此,请输入:
¥If you are using CentOS 8, you must also disable the system's PostgreSQL module to prevent it from interfering with the repository's PostgreSQL version. To do so, type:
sudo yum module disable postgresql
接下来,选择你想要的目标 PostgreSQL 版本。
¥Next, choose the version of PostgreSQL that you wish to target.
对于 CentOS,请输入以下命令检查可用的 PostgreSQL 版本:
¥For CentOS, check which versions of PostgreSQL are available by typing:
yum list postgresql*-server
对于 Fedora,请使用 dnf 包管理器:
¥For Fedora use the dnf package manager:
dnf list postgresql*-server
确定要使用的版本后,你可以使用包管理器进行安装。
¥After deciding which version to use, you can install it using your package manager.
对于 CentOS,请使用 yum 包管理器。例如,要安装 PostgreSQL 12,请输入:
¥For CentOS use the yum package manager. For example, to install PostgreSQL 12, type:
sudo yum install postgresql12-server
对于 Fedora,请使用 dnf 包管理器。例如,要安装 PostgreSQL 12,请输入:
¥For Fedora use the dnf package manager. For example, to install PostgreSQL 12, type:
sudo dnf install postgresql12-server
安装 PostgreSQL 软件包后,请初始化数据库:
¥Once the PostgreSQL package is installed, initialize the database:
sudo /usr/pgsql-*/bin/postgresql-*-setup initdb
查找适用于你的 PostgreSQL 版本的 systemd 单元文件的名称:
¥Find the name of the systemd unit file for your version of PostgreSQL:
systemctl list-unit-files | grep postgresql
使用你找到的单元文件启动服务。例如,对于 PostgreSQL 12,它将是:
¥Start up the service using the unit file you found. For example, for PostgreSQL 12, it would be:
sudo systemctl start postgresql-12.service
你也可以选择使用 enable 在系统启动时自动启动 PostgreSQL:
¥Optionally, you can also automatically start PostgreSQL on boot with the enable:
sudo systemctl enable postgresql-12.service
默认情况下,PostgreSQL 配置为使用 对等身份验证,如果用户的操作系统用户名与 PostgreSQL 内部名称匹配,则允许用户登录。
¥By default, PostgreSQL is configured to use peer authentication, which allows users to log in if their operating system user name matches a PostgreSQL internal name.
安装过程创建了一个名为 postgres 的操作系统用户,以匹配 postgres 数据库管理账户。要为 psql 用户打开 shell 会话并登录数据库,你可以输入:
¥The installation process created an operating system user called postgres to match the postgres database administrative account. To log into PostgreSQL with the psql client, use sudo to run the command as the postgres user:
sudo -u postgres psql
完成后,你可以输入以下命令退出 psql 会话:
¥When you are finished, you can exit the psql session by typing:
\quit