在 Ubuntu 22.04|20.04|18.04 上安装 PostgreSQL 13
PostgreSQL 是一种非常流行的开源对象关系数据库管理系统 (DBMS),旨在保证可靠性、效率和数据完整性。开发工作现已超过 30 年,该项目在可靠性、功能稳健性和性能方面赢得了良好的声誉。在本教程中,我们将解释如何在 Ubuntu 22.04|20.04|18.04 Linux 服务器上安装和配置 PostgreSQL 13 数据库服务器。
数千家公司使用 PostgreSQL 来支持支付交易、大量网站流量、电子商务平台等。它还允许您添加使用 Java、Python、C/C++ 等编程语言开发的自定义函数。
截至本文发布时,PostgreSQL 的最新版本为 17,旨在用于在生产环境中运行工作负载。您可以在 发行说明中找到有关 PostgreSQL 13 中的所有功能和更改的信息。
步骤 1:更新 Ubuntu 系统
我们始终致力于最新版本的操作系统,以确保不存在旧的依赖性问题。登录到您的 Ubuntu 服务器并运行以下命令来更新所有已安装的软件包。
sudo apt update && sudo apt -y full-upgrade
[ -f /var/run/reboot-required ] && sudo reboot -f
系统更新后,我建议您重新启动以运行新的内核(如果已更新)。
sudo reboot
步骤 2:添加 PostgreSQL 13 存储库
安装所需的依赖包
sudo apt update
sudo apt install curl gpg gnupg2 software-properties-common apt-transport-https lsb-release ca-certificates
现在我们已经更新并重新启动了系统,让我们添加从 PostgreSQL 存储库中提取包所需的 APT 存储库。
curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc|sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/postgresql.gpg
导入 GPG 密钥后,将存储库内容添加到您的 Ubuntu 22.04|20.04|18.04 系统:
echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" |sudo tee /etc/apt/sources.list.d/pgdg.list
添加的存储库包含许多不同的软件包,包括第三方插件。它们包括:
postgresql-client
postgresql
libpq-dev
postgresql-server-dev
pgadmin packages
步骤3:安装PostgreSQL 13
添加存储库后,我们可以在 Ubuntu 22.04|20.04|18.04 Linux 服务器上安装 PostgreSQL 13 软件包。但首先要更新软件包索引,以便在操作系统级别可以使用该版本。
sudo apt update
运行以下命令在 Ubuntu 22.04|20.04|18.04 Linux 系统上安装 PostgreSQL 13。
sudo apt install postgresql-13 postgresql-client-13
PostgreSQL 服务已启动并设置为每次系统重启后运行。
$ systemctl status postgresql@13-main.service
● postgresql@13-main.service - PostgreSQL Cluster 13-main
Loaded: loaded (/lib/systemd/system/postgresql@.service; enabled-runtime; vendor preset: enabled)
Active: active (running) since Wed 2023-05-18 15:50:22 EAT; 1min 55s ago
Process: 4294 ExecStart=/usr/bin/pg_ctlcluster --skip-systemctl-redirect 13-main start (code=exited, status=0/SUCCESS)
Main PID: 4299 (postgres)
Tasks: 7 (limit: 9460)
Memory: 18.2M
CPU: 150ms
CGroup: /system.slice/system-postgresql.slice/postgresql@13-main.service
├─4299 /usr/lib/postgresql/13/bin/postgres -D /var/lib/postgresql/13/main -c config_file=/etc/postgresql/13/main/postgresql.conf
├─4301 "postgres: 13/main: checkpointer " "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ">
├─4302 "postgres: 13/main: background writer " "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "">
├─4303 "postgres: 13/main: walwriter " "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ">
├─4304 "postgres: 13/main: autovacuum launcher " "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" >
├─4305 "postgres: 13/main: stats collector " "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ">
└─4306 "postgres: 13/main: logical replication launcher " "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" >
Mei 18 15:50:19 ubuntu22 systemd[1]: Starting PostgreSQL Cluster 13-main...
Mei 18 15:50:22 ubuntu22 systemd[1]: Started PostgreSQL Cluster 13-main.
步骤 4:测试 PostgreSQL 连接
安装过程中会自动创建一个 postgres 用户。此用户对整个 PostgreSQL 实例具有完全 超级管理员 访问权限。在切换到此帐户之前,您登录的系统用户应该具有 sudo 权限。
sudo su - postgres
让我们将此用户密码重置为我们可以记住的强密码。
psql -c "alter user postgres with password 'StrongAdminP@ssw0rd'"
使用以下命令启动 PostgreSQL 提示符:
$ psql
获取如下所示的连接详细信息。
$ psql
psql (13.7 (Ubuntu 13.7-1.pgdg22.04+1))
Type "help" for help.
postgres=# \conninfo
You are connected to database "postgres" as user "postgres" via socket in "/var/run/postgresql" at port "5432".
让我们创建一个测试数据库和用户来看看它是否正常工作。
postgres=# CREATE DATABASE mytestdb;
CREATE DATABASE
postgres=# CREATE USER mytestuser WITH ENCRYPTED PASSWORD 'MyStr0ngP@SS';
CREATE ROLE
postgres=# GRANT ALL PRIVILEGES ON DATABASE mytestdb to mytestuser;
GRANT
列出创建的数据库:
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+---------+---------+-------------------------
mytestdb | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =Tc/postgres +
| | | | | postgres=CTc/postgres +
| | | | | mytestuser=CTc/postgres
postgres | postgres | UTF8 | C.UTF-8 | C.UTF-8 |
template0 | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
连接到数据库:
postgres-# \c mytestdb
You are now connected to database "mytestdb" as user "postgres".
安装的其他 PostgreSQL 实用程序(如 createuser 和 createdb) 可用于创建数据库和用户。
postgres@ubuntu:~$ createuser myuser --password
Password:
postgres@ubuntu:~$ createdb mydb -O myuser
postgres@ubuntu:~$ psql -l
我们可以在 PostgreSQL 服务器上创建并连接到数据库。
步骤 5:配置远程连接(可选)
在 Ubuntu 上安装 PostgreSQL 13 仅接受来自本地主机的连接。在理想的生产环境中,您将拥有一个中央数据库服务器和与其连接的远程客户端 - 但当然是在 专用网络 (LAN) 内。
要启用远程连接,请编辑 PostgreSQL 配置文件:
sudo nano /etc/postgresql/13/main/postgresql.conf
取消注释第 59行 并更改监听地址以接受网络内的连接。
# Listen on all interfaces
listen_addresses = '*'
# Listen on specified private IP address
listen_addresses = '192.168.10.11'
还设置 PostgreSQL 以接受来自允许主机的远程连接。
$ sudo nano /etc/postgresql/13/main/pg_hba.conf
# Accept from anywhere
host all all 0.0.0.0/0 md5
# Accept from trusted subnet
host all all 10.10.10.0/24 md5
更改后,重新启动postgresql服务。
sudo systemctl restart postgresql
确认监听地址。
# netstat -tunelp | grep 5432
tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN 111 112837 11143/postgres
tcp6 0 0 :::5432 :::* LISTEN 111 112838 11143/postgres
步骤6:安装pgAdmin4管理工具
如果您想从 Web 界面管理 PostgreSQL 数据库服务器,请安装 pgAdmin4。
在 Ubuntu 上安装 pgAdmin4
如果您喜欢此文章,请收藏、点赞、评论,谢谢,祝您快乐每一天。