使用 systemctl 启动 PostgreSQL 服务时权限被拒绝,但与 postmaster 一起使用

使用 systemctl 启动 PostgreSQL 服务时权限被拒绝,但与 postmaster 一起使用

我之前通过以下方式安装了 PostgreSQL本文档然后我使用以下命令卸载了它(我在 Fedora 上)

sudo rm -rf /var/lib/pgsql/
sudo dnf remove postgresql postgresql-server

之后我尝试再次重新安装它,但将默认端口从更改54325433

sudo dnf install postgresql postgresql-server
sudo postgresql-setup --initdb --unit postgresql --port 5433

/var/lib/pgsql/data/postgresql.conf文件确实有port = 5433未注释的行

但是当我尝试使用启动服务时sudo systemctl start postgresql,出现以下错误

Job for postgresql.service failed because the control process exited with error code.
See "systemctl status postgresql.service" and "journalctl -xeu postgresql.service" for details.

以下是该文件夹中的日志log

2023-05-04 10:46:56.034 CEST [6340] LOG:  starting PostgreSQL 15.1 on x86_64-redhat-linux-gnu, compiled by gcc (GCC) 13.0.1 20230117 (Red Hat 13.0.1-0), 64-bit
2023-05-04 10:46:56.034 CEST [6340] LOG:  could not bind IPv6 address "::1": Permission denied
2023-05-04 10:46:56.034 CEST [6340] LOG:  could not bind IPv4 address "127.0.0.1": Permission denied
2023-05-04 10:46:56.034 CEST [6340] WARNING:  could not create listen socket for "localhost"
2023-05-04 10:46:56.034 CEST [6340] FATAL:  could not create any TCP/IP sockets
2023-05-04 10:46:56.036 CEST [6340] LOG:  database system is shut down

但是当我启动 PostgreSQL 而不使用postmaster它时它就可以工作

sudo su - postgres
/usr/bin/postmaster -D /var/lib/pgsql/data

这是日志:

2023-05-04 11:08:18.997 CEST [9385] LOG:  starting PostgreSQL 15.1 on x86_64-redhat-linux-gnu, compiled by gcc (GCC) 13.0.1 20230117 (Red Hat 13.0.1-0), 64-bit
2023-05-04 11:08:18.998 CEST [9385] LOG:  listening on IPv6 address "::1", port 5433
2023-05-04 11:08:18.998 CEST [9385] LOG:  listening on IPv4 address "127.0.0.1", port 5433
2023-05-04 11:08:19.000 CEST [9385] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5433"
2023-05-04 11:08:19.002 CEST [9385] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5433"
2023-05-04 11:08:19.009 CEST [9389] LOG:  database system was shut down at 2023-05-04 10:46:43 CEST
2023-05-04 11:08:19.030 CEST [9385] LOG:  database system is ready to accept connections

/usr/lib/systemd/system有两个与 PostgreSQL 相关的服务

-rw-r--r--. 1 root root 1546 20 janv. 01:00 postgresql.service
-rw-r--r--. 1 root root 1507 20 janv. 01:00 [email protected]

我检查了内容,对我来说没有什么奇怪的。

postgresql.conf文件中,如果我将端口更改为5432那么它就可以工作,我可以成功启动服务!

我检查了是否有任何东西已经使用5433命令使用该端口netstat -aon | grep 5433,但似乎没有

知道更改默认端口时可能会导致此问题的原因是什么吗?我对 Linux 有点陌生,对服务也不熟悉。

感谢您的回答

答案1

好吧,事实证明是 SELinux 阻止了它(再次!)。

这是我在 SELinux 日志文件中找到的日志:

~ $ sudo grep "postgre" /var/log/audit/audit.log | grep "5433"
type=AVC msg=audit(1683186096.646:1250): avc:  denied  { name_bind } for  pid=56692 comm="postmaster" src=5433 scontext=system_u:system_r:postgresql_t:s0 tcontext=system_u:object_r:unreserved_port_t:s0 tclass=tcp_socket permissive=0

5432仅限9898SELinux 允许的端口

~ $ sudo semanage port -l | grep postgresql
postgresql_port_t              tcp      5432, 9898

所以我只需要将端口添加5433到列表中:

~ $ sudo semanage port -a -t postgresql_port_t 5433 -p tcp
~ $ sudo semanage port -l | grep postgresql
postgresql_port_t              tcp      5433, 5432, 9898

来源

之后启动服务即可:

~ $ sudo systemctl start postgresql
~ $ sudo cat /var/lib/pgsql/data/log/postgresql-Thu.log
2023-05-04 12:34:30.786 CEST [14231] LOG:  starting PostgreSQL 15.1 on x86_64-redhat-linux-gnu, compiled by gcc (GCC) 13.0.1 20230117 (Red Hat 13.0.1-0), 64-bit
2023-05-04 12:34:30.786 CEST [14231] LOG:  listening on IPv6 address "::1", port 5433
2023-05-04 12:34:30.786 CEST [14231] LOG:  listening on IPv4 address "127.0.0.1", port 5433
2023-05-04 12:34:30.788 CEST [14231] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5433"
2023-05-04 12:34:30.791 CEST [14231] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5433"
2023-05-04 12:34:30.799 CEST [14235] LOG:  database system was shut down at 2023-05-04 12:34:05 CEST
2023-05-04 12:34:30.824 CEST [14231] LOG:  database system is ready to accept connections

相关内容