我之前通过以下方式安装了 PostgreSQL本文档然后我使用以下命令卸载了它(我在 Fedora 上)
sudo rm -rf /var/lib/pgsql/
sudo dnf remove postgresql postgresql-server
之后我尝试再次重新安装它,但将默认端口从更改5432
为5433
:
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
仅限9898
SELinux 允许的端口
~ $ 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