在 Ubuntu 22.04|20.04|18.04 | 安装 PostgreSQL 13在 Ubuntu 22.04|20.04|18.04 | 安装 PostgreSQL 13在 Ubuntu 22.04|20.04|18.04 | 安装 PostgreSQL 13在 Ubuntu 22.04|20.04|18.04 | 安装 PostgreSQL 13
  • 业务
  • 目标
  • 支持
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容

在 Ubuntu 22.04|20.04|18.04 | 安装 PostgreSQL 13

发表 admin at 2025年2月28日
类别
  • 未分类
标签

PostgreSQL 是一个非常流行的开源对象关系数据库管理系统(DBMS),旨在保证可靠性、效率和数据完整性。开发工作现已超过 30 年,该项目在可靠性、功能稳健性和性能方面赢得了良好的声誉。在本教程中,我们将解释如何在 Ubuntu 22.04|20.04|18.04 Linux 服务器上安装和配置 PostgreSQL 13 数据库服务器。

PostgreSQL 被数千家公司用来支持支付交易、巨大的网站流量、电子商务平台等。它还使您能够添加使用 Java、Python、C/C++ 等编程语言开发的自定义函数。

截至本文更新,PostgreSQL 的最新版本为 13,旨在用于在生产环境中运行工作负载。您可以在发行说明中找到有关 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 包

步骤 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 [email 
● [email  - PostgreSQL Cluster 13-main
     Loaded: loaded (/lib/systemd/system/[email ; enabled-runtime; vendor preset: enabled)
     Active: active (running) since Wed 2022-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/[email 
             ├─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

享受在 Ubuntu 22.04|20.04|18.04 上使用 PostgreSQL 13 的乐趣。与数据库相关的其他指南在下面的列表中共享。

  • 如何在 Ubuntu 上安装 PostGIS
  • 在 CentOS 7 上安装 PostgreSQL 13
  • 在 CentOS 8/RHEL 8 上安装 PostgreSQL

标签:

  • 在 Ubuntu 22.04|20.04|18.04 上安装 PostgreSQL 13
  • 在 Ubuntu 上安装 PostgreSQL 13
©2015-2025 Norria support@norria.com