如何在Fedora 33/32/31/30上安装PostgreSQL 12

时间:2020-02-23 14:32:11  来源:igfitidea点击:

本教程将讨论在Fedora 33/32/31/30上PostgreSQL 12的安装。 PostgreSQL是一种开放源代码数据库服务器,其创建目的是可靠,健壮并适用于注重性能的部署。 PostgreSQL 12已于2019年10月3日发布,具有许多新功能以供一般使用。

让我们开始在Fedora 33/32/31/30上安装PostgreSQL 12.

更新系统

确保系统软件包是最新的:

sudo dnf install -y vim bash-completion wget
sudo dnf update -y

由于我们可能具有内核更新,因此建议我们在升级后重新引导系统:

sudo reboot

添加PostgreSQL Yum存储库

通过运行以下命令将PostgreSQL Yum存储库添加到Fedora系统:

Fedora 33:

sudo dnf install https://download.postgresql.org/pub/repos/yum/reporpms/F-33-x86_64/pgdg-fedora-repo-latest.noarch.rpm

Fedora 32:

sudo dnf install https://download.postgresql.org/pub/repos/yum/reporpms/F-32-x86_64/pgdg-fedora-repo-latest.noarch.rpm

Fedora 31:

sudo dnf install https://download.postgresql.org/pub/repos/yum/reporpms/F-31-x86_64/pgdg-fedora-repo-latest.noarch.rpm

Fedora 30:

sudo dnf install https://download.postgresql.org/pub/repos/yum/reporpms/F-30-x86_64/pgdg-fedora-repo-latest.noarch.rpm

Fedora 29:

sudo dnf install https://download.postgresql.org/pub/repos/yum/reporpms/F-29-x86_64/pgdg-fedora-repo-latest.noarch.rpm

当提示我们确认安装时,请按它们。

Last metadata expiration check: 0:01:25 ago on Sat 12 Oct 2019 06:52:54 AM UTC.
pgdg-fedora-repo-latest.noarch.rpm                                                                                  21 kB/s | 9.7 kB     00:00    
Dependencies resolved.
===================================================================================================================================================
 Package                                  Architecture                   Version                        Repository                            Size
===================================================================================================================================================
Installing:
 pgdg-fedora-repo                         noarch                         42.0-5                         @commandline                         9.7 k

Transaction Summary
===================================================================================================================================================
Install  1 Package

Total size: 9.7 k
Installed size: 9.7 k
Is this ok [y/N]: y
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                           1/1 
  Installing       : pgdg-fedora-repo-42.0-5.noarch                                                                                            1/1 
  Verifying        : pgdg-fedora-repo-42.0-5.noarch                                                                                            1/1 

Installed:
  pgdg-fedora-repo-42.0-5.noarch                                                                                                                   

Complete!

在Fedora 33/32/31/30上安装PostgreSQL 12

将PostgreSQL信息库添加到Fedora系统后,安装PostgreSQL 12 Server/Client软件包:

sudo dnf install postgresql12-server postgresql12

同意安装。

Last metadata expiration check: 0:01:17 ago on Sat 12 Oct 2019 07:00:29 AM UTC.
Dependencies resolved.
===================================================================================================================================================
 Package                                   Architecture                 Version                                 Repository                    Size
===================================================================================================================================================
Installing:
 postgresql12-server                       x86_64                       12.0-1PGDG.f30                          pgdg12                       5.1 M
Installing dependencies:
 postgresql12                              x86_64                       12.0-1PGDG.f30                          pgdg12                       1.5 M
 postgresql12-libs                         x86_64                       12.0-1PGDG.f30                          pgdg12                       393 k

Transaction Summary
===================================================================================================================================================
Install  3 Packages

Total download size: 7.0 M
Installed size: 32 M
Is this ok [y/N]: y

初始化数据库并启用自动启动

现在已经安装了数据库软件包,通过运行以下命令来初始化数据库

$sudo /usr/pgsql-12/bin/postgresql-12-setup initdb
Initializing database ... OK

然后启动并启用服务以在启动时启动

sudo systemctl enable --now postgresql-12

确认服务状态。

$systemctl status postgresql-12
● postgresql-12.service - PostgreSQL 12 database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql-12.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2019-10-12 07:08:57 UTC; 12s ago
     Docs: https://www.postgresql.org/docs/12/static/
  Process: 1506 ExecStartPre=/usr/pgsql-12/bin/postgresql-12-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
 Main PID: 1511 (postmaster)
    Tasks: 8 (limit: 2354)
   Memory: 15.9M
   CGroup: /system.slice/postgresql-12.service
           ├─1511 /usr/pgsql-12/bin/postmaster -D /var/lib/pgsql/12/data/
           ├─1512 postgres: logger   
           ├─1514 postgres: checkpointer   
           ├─1515 postgres: background writer   
           ├─1516 postgres: walwriter   
           ├─1517 postgres: autovacuum launcher   
           ├─1518 postgres: stats collector   
           └─1519 postgres: logical replication launcher   

Oct 12 07:08:57 fed30.novalocal systemd[1]: Starting PostgreSQL 12 database server...
Oct 12 07:08:57 fed30.novalocal postmaster[1511]: 2019-10-12 07:08:57.760 UTC [1511] LOG:  starting PostgreSQL 12.0 on x86_64-pc-linux-gnu, compil>
Oct 12 07:08:57 fed30.novalocal postmaster[1511]: 2019-10-12 07:08:57.762 UTC [1511] LOG:  listening on IPv6 address "::1", port 5432
Oct 12 07:08:57 fed30.novalocal postmaster[1511]: 2019-10-12 07:08:57.762 UTC [1511] LOG:  listening on IPv4 address "127.0.0.1", port 5432
Oct 12 07:08:57 fed30.novalocal postmaster[1511]: 2019-10-12 07:08:57.767 UTC [1511] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.>
Oct 12 07:08:57 fed30.novalocal postmaster[1511]: 2019-10-12 07:08:57.774 UTC [1511] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
Oct 12 07:08:57 fed30.novalocal postmaster[1511]: 2019-10-12 07:08:57.782 UTC [1511] LOG:  redirecting log output to logging collector process
Oct 12 07:08:57 fed30.novalocal postmaster[1511]: 2019-10-12 07:08:57.782 UTC [1511] HINT:  Future log output will appear in directory "log".
Oct 12 07:08:57 fed30.novalocal systemd[1]: Started PostgreSQL 12 database server.

如果我们具有正在运行的防火墙服务,并且远程客户端应连接到数据库服务器,则允许PostgreSQL服务。

sudo firewall-cmd --add-service=postgresql --permanent
sudo firewall-cmd --reload

启用对PostgreSQL的远程访问(可选)

如果我们有"应用程序"通过网络连接到数据库服务器,请编辑文件" /var/lib/pgsql/12/data/postgresql.conf",并将"侦听地址"设置为服务器IP地址,或者将" *"设置为所有接口。

$sudo vim /var/lib/pgsql/12/data/postgresql.conf 
listen_addresses = '*'

还要将PostgreSQL设置为接受来自允许的主机的远程连接。

$sudo vim /var/lib/pgsql/11/data/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 12服务。

sudo systemctl restart postgresql-12

设置PostgreSQL管理员用户密码

设置PostgreSQL管理员用户密码。

$sudo su - postgres
$psql -c "alter user postgres with password 'StrongPassword'"

其中StrongPassword是postgres用户的新密码。

安装pgAdmin 4 Web管理

如果我们需要一种简单的方法来管理PostgreSQL数据库服务器(类似于MySQL的phpMyAdmin),请考虑安装pgAdmin4.

在Fedora上安装pgAdmin 4