1

I previously installed PostgreSQL by following this documentation then I uninstalled it using the following commands (I'm on Fedora)

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

After that I tried to reinstall it again but changing the default port from 5432 to 5433:

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

The /var/lib/pgsql/data/postgresql.conf file does indeed have the line port = 5433 uncommented

But when I try to start the service using sudo systemctl start postgresql, I get the following error

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.

Here are the logs from the log folder:

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

However when I start PostgreSQL without using postmaster it works

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

Here are the logs:

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

In /usr/lib/systemd/system I have two services related to 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 postgresql@.service

I checked to content and nothing seems weird to me.

In the postgresql.conf file if I change the port to 5432 then it works, I can successfully start the service!

I checked if there's anything already using the port 5433 with the command netstat -aon | grep 5433 but it seems not

Any idea what could cause this problem when changing the default port? I'm kind of new to Linux and I'm unfamiliar with services.

Thanks for your answers

  • Hi, I suspect a problem with the systemd service file. Does this help you ?
    Have a look at this command ExecStart=/usr/local/pgsql/bin/pg_ctl -o "-p 5489"
    – ramius May 04 '23 at 10:26
  • Thanks for your comment. Actually I just found the issue, it was SELinux blocking it! I just posted an answer how to fix it – Jérôme MEVEL May 04 '23 at 10:37

1 Answers1

2

Ok so turns out it was SELinux blocking it (again!).

Here is a log I found in SELinux log file:

~ $ 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

Only ports 5432 and 9898 were allowed by SELinux

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

So I just needed to add the port 5433 to the list:

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

source

After that starting the service works:

~ $ 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