使用yum存储库从AlmaLinux 8安装PostgreSQL 14
什么? ?)
按照标题所说,我们将使用AlmaLinx 8上的PostgreSQL的yum仓库来安装PostgreSQL 14。
环境
这次的环境在这里。
$ cat /etc/redhat-release
AlmaLinux release 8.5 (Arctic Sphynx)
$ uname -srvmpio
Linux 4.18.0-348.23.1.el8_5.x86_64 #1 SMP Wed Apr 27 05:47:31 EDT 2022 x86_64 x86_64 x86_64 GNU/Linux
请确认安装方法。
首先,我们来确认安装方法。
在这个页面上选择”Linux”,然后从出现的选项中选择”Red Hat/Rocky/CentOS”。
然后,您会转到这个页面,只需按照步骤进行即可。
这个页面所针对的是以下内容,不过在AlmaLinux 8上也应该还可以,我想。
-
- Red Hat Enterprise Linux
-
- Rocky Linux
-
- CentOS 7または6
-
- Fedora
- Oracle Linux
顺便提一下,如果你想找RPM,可以试试这个链接。
从yum仓库安装PostgreSQL 14。
那么,让我们开始安装吧。
安装 Yum 仓库。
$ sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
接下来,禁用默认的PostgreSQL模块。
$ sudo dnf -qy module disable postgresql
顺便提一下,如果不禁用的话,无法看到新增的 yum 存储库中的 PostgreSQL。
$ sudo dnf install postgresql14-server
Last metadata expiration check: 0:00:05 ago on 2022年04月29日 22時04分39秒.
All matches were filtered out by modular filtering for argument: postgresql14-server
Error: Unable to find a match: postgresql14-server
安装PostgreSQL 14服务器。
$ sudo dnf install postgresql14-server
本次版本是PostgreSQL 14.2。
$ psql --version
psql (PostgreSQL) 14.2
包裹的信息。
$ dnf info postgresql14-server
Last metadata expiration check: 0:03:34 ago on 2022年04月29日 22時02分59秒.
Installed Packages
Name : postgresql14-server
Version : 14.2
Release : 1PGDG.rhel8
Architecture : x86_64
Size : 23 M
Source : postgresql14-14.2-1PGDG.rhel8.src.rpm
Repository : @System
From repo : pgdg14
Summary : The programs needed to create and run a PostgreSQL server
URL : https://www.postgresql.org/
License : PostgreSQL
Description : PostgreSQL is an advanced Object-Relational database management system (DBMS).
: The postgresql14-server package contains the programs needed to create
: and run a PostgreSQL server, which will in turn allow you to create
: and maintain PostgreSQL databases.
接下来,将进行数据库的初始化。
$ sudo PGSETUP_INITDB_OPTIONS='--encoding=UTF-8 --no-locale' /usr/pgsql-14/bin/postgresql-14-setup initdb
使用PGSETUP_INITDB_OPTIONS环境变量,编码为UTF-8,没有设置区域设置。
地域设置已更改为C。
$ sudo grep lc_ /var/lib/pgsql/14/data/postgresql.conf
lc_messages = 'C' # locale for system error message
lc_monetary = 'C' # locale for monetary formatting
lc_numeric = 'C' # locale for number formatting
lc_time = 'C' # locale for time formatting
如果按照此文件进行 sudo /usr/pgsql-14/bin/postgresql-14-setup initdb 命令,会使得地域设置变为当前环境的设置。
$ sudo /usr/pgsql-14/bin/postgresql-14-setup initdb
Initializing database ... OK
$ sudo grep lc_ /var/lib/pgsql/14/data/postgresql.conf
# - Locale and Formatting -
lc_messages = 'ja_JP.utf8' # locale for system error message
lc_monetary = 'ja_JP.utf8' # locale for monetary formatting
lc_numeric = 'ja_JP.utf8' # locale for number formatting
lc_time = 'ja_JP.utf8' # locale for time formatting
启用自动启动。
$ sudo systemctl enable postgresql-14
状态。
$ sudo systemctl status postgresql-14
● postgresql-14.service - PostgreSQL 14 database server
Loaded: loaded (/usr/lib/systemd/system/postgresql-14.service; enabled; vendor preset: disabled)
Active: inactive (dead)
Docs: https://www.postgresql.org/docs/14/static/
开始。
$ sudo systemctl start postgresql-14
查看状态。
$ sudo systemctl status postgresql-14
● postgresql-14.service - PostgreSQL 14 database server
Loaded: loaded (/usr/lib/systemd/system/postgresql-14.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2022-04-29 22:10:52 JST; 10s ago
Docs: https://www.postgresql.org/docs/14/static/
Process: 4022 ExecStartPre=/usr/pgsql-14/bin/postgresql-14-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
Main PID: 4027 (postmaster)
Tasks: 8 (limit: 12442)
Memory: 16.6M
CGroup: /system.slice/postgresql-14.service
├─4027 /usr/pgsql-14/bin/postmaster -D /var/lib/pgsql/14/data/
├─4029 postgres: logger
├─4031 postgres: checkpointer
├─4032 postgres: background writer
├─4033 postgres: walwriter
├─4034 postgres: autovacuum launcher
├─4035 postgres: stats collector
└─4036 postgres: logical replication launcher
4月 29 22:10:52 localhost.localdomain systemd[1]: Starting PostgreSQL 14 database server...
4月 29 22:10:52 localhost.localdomain postmaster[4027]: 2022-04-29 22:10:52.411 JST [4027] LOG: redirecting log output to logging collector process
4月 29 22:10:52 localhost.localdomain postmaster[4027]: 2022-04-29 22:10:52.411 JST [4027] HINT: Future log output will appear in directory "log".
4月 29 22:10:52 localhost.localdomain systemd[1]: Started PostgreSQL 14 database server.
数据等存放在/var/lib/pgsql/14目录下。
$ sudo ls -l /var/lib/pgsql/14
total 8
drwx------. 2 postgres postgres 6 2月 9 17:13 backups
drwx------. 20 postgres postgres 4096 4月 29 22:10 data
-rw-------. 1 postgres postgres 1260 4月 29 22:09 initdb.log
确认动作
让我们确认一下动作。
切换到postgres用户
$ sudo su - postgres
在psql中进行登录,并进行版本确认。
$ psql
psql (14.2)
"help"でヘルプを表示します。
postgres=# select version();
version
--------------------------------------------------------------------------------------------------------
PostgreSQL 14.2 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-4), 64-bit
(1 行)
创建用户和数据库。
postgres=# create user myuser password 'password';
CREATE ROLE
postgres=# create database example owner myuser;
CREATE DATABASE
我创建了一个用户,想通过网络访问,但使用-h选项访问时被拒绝连接。
※服务器的IP地址是192.168.33.10。
$ psql -U myuser -h 192.168.33.10 example
psql: エラー: "192.168.33.10"、ポート5432でのサーバーへの接続に失敗しました: 接続を拒否されました
サーバーはそのホストで動作していて、TCP/IP接続を受け付けていますか?
网络连接仅限于本地许可。
$ ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 127.0.0.1:5432 0.0.0.0:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 128 [::1]:5432 [::]:*
为了能够连接,请进行PostgreSQL的配置更改。
修改PostgreSQL的设置
首先,确认/var/lib/pgsql/14/data/postgresql.conf文件中的listen_addresses设置。
#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------
# - Connection Settings -
#listen_addresses = 'localhost' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
我們解除註釋,使其接受來自其他伺服器的連接。
#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------
# - Connection Settings -
listen_addresses = '*' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
接着,将更改/var/lib/pgsql/14/data/pg_hba.conf。
host all all 192.168.33.0/24 scram-sha-256
重新启动PostgreSQL。
$ sudo systemctl restart postgresql-14
现在可以通过这个访问了。
$ psql -U myuser -h 192.168.33.10 example
ユーザー myuser のパスワード:
psql (14.2)
"help"でヘルプを表示します。
example=>