How to change the TCP port number of Postgresql in the firewall

1

Hi, I will explain the problem in detail. I have a web application deployed over https in my production environment on Amazon. This application uses a Postgresql database on port 5432. The application has been working correctly since the beginning and it's been over 3 years. However, for a few days I could no longer access my application. I then checked the database and realized that Postgresql was no longer working on port 5432. I then restarted the port with the service postgresql restart command and suddenly the application worked again. But the next day, the problem returned and I restarted Postgres but this time the database refused to start. So I'm now trying to modify Postgresql port 5432 to see if that would solve my problem. And since I opened the port in my Amazon instance, that's where I should be able to modify it, which I can't. IF I run this command sudo -u postgres psql, I GET ERROR: psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory Is the server running locally and accepting connections on that socket?

  • Can you please write question in more detail, where is your Postgres’s and what is the firewall ?

  • If the input field for modifying the firewall rule is inactive, it's possible that your user account may not have the necessary permissions to make changes to firewall settings. You may need to check if you have the appropriate permissions or if there are any restrictions in place that prevent modifications

Joseph
asked 13 days ago99 views
2 Answers
0
Accepted Answer

I doubt that changing the port will make any difference...

You may want to review the memory usage of the EC2 and see if its being exhausted. You may also want to review the Server logs to see why its stopping.

profile picture
EXPERT
answered 13 days ago
  • Thanks Gary. My web server (Apache TomEE) is working normally. The problem is only related to the Postgres database. I will try to see, as you said, if it is a problem of memory usage of EC2.

  • Thanks Gary. My web server (Apache TomEE) is working normally. The problem is only related to the Postgres database. I will try to see, as you said, if it is a problem of memory usage of EC2.

  • Thanks Gary. My web server (Apache TomEE) is working normally. The problem is only related to the Postgres database. I will try to see, as you said, if it is a problem of memory usage of EC2.

  • Thanks Gary. My web server (Apache TomEE) is working normally. The problem is only related to the Postgres database. I will try to see, as you said, if it is a problem of memory usage of EC2.

  • Thanks Gary. My web server (Apache TomEE) is working normally. The problem is only related to the Postgres database. I will try to see, as you said, if it is a problem of memory usage of EC2.

0
  • Verify Port Status: Before attempting to modify the port, confirm whether port 5432 is currently open or closed. You can use tools like telnet, netstat, or firewall management interfaces to check the port status.

  • Check for Conflicts: Ensure that there are no other applications or services currently using port 5432. Port conflicts can prevent PostgreSQL from binding to the specified port.

-here's a step-by-step guide to changing the TCP port number for PostgreSQL in Linux:

Check Current Port Configuration: First, confirm the current port configuration for PostgreSQL. You can typically find this information in the PostgreSQL configuration file (postgresql.conf). Look for the port directive. Alternatively, you can use the netstat command to check which port PostgreSQL is currently listening on: netstat -tuln | grep postgres

Identify Firewall Software: Determine which firewall software you are using on your Linux system. Common firewall software on Linux includes iptables (legacy) and firewalld (more common in recent distributions). You'll use different commands depending on the firewall software.

Modify Firewall Rules: Let's assume you're using firewalld which is common on modern Linux distributions like Fedora, CentOS, and RHEL. To modify the firewall rules, first, identify the service associated with PostgreSQL: firewall-cmd --get-services | grep postgres

Open New Port: If there isn't a predefined service for PostgreSQL, you can manually add the port: firewall-cmd --zone=public --add-port=5432/tcp --permanent

Reload Firewall Configuration: After adding the new port, reload the firewall configuration to apply the changes: firewall-cmd --reload

Verify Changes: Check if the new port is now open in the firewall: firewall-cmd --zone=public --list-ports

Update PostgreSQL Configuration: Open the PostgreSQL configuration file (postgresql.conf) with a text editor like nano or vi: sudo nano /etc/postgresql/<version>/main/postgresql.conf Find the port directive and change the port number to 5432 or the new port you specified.

Restart PostgreSQL Service: After updating the configuration, restart the PostgreSQL service for the changes to take effect: sudo systemctl restart postgresql

Test Connectivity: Verify that PostgreSQL is now listening on the new port: netstat -tuln | grep postgres Additionally, test connectivity to PostgreSQL from remote hosts using the new port.

answered 13 days ago
profile picture
EXPERT
reviewed 13 days ago
  • Thanks! I will try to apply your recommendations and I will let you know what happens.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions