PostgreSQL is one of the most powerful and widely used relational database management systems (RDBMS) in the world. By default, PostgreSQL listens for incoming connections on port 5432.
In this article, we will describe the process of changing the default port for PostgreSQL in detailed and step-by-step manner from basic configurations to advanced setups.
1. Where is the Default Port Defined?
The default port for PostgreSQL is specified in the Postgresql.conf configuration file. This file controls various settings for the PostgreSQL server, including the port it listens on.
- By default, the port is set to 5432, but we can change it to any other port number (within the valid range of 1024 to 65535) according to our needs.
2. Why Change the Default Port?
There are several reasons we might want to change the default port (5432) for the PostgreSQL instance:
Security
Using a non-default port can add an additional layer of "security through obscurity". While this does not replace proper security practices, it can help reduce exposure to automated attacks or unwanted traffic.
Running Multiple PostgreSQL Instances
If we want to run multiple instances of PostgreSQL on the same server (for example, development, staging, and production databases), we will need to assign different ports to each instance.
Port Conflicts
Sometimes, other services may already use port 5432 (such as another PostgreSQL instance, or a different database service). In such cases, changing the port is a simple way to avoid conflicts.
Compliance or Networking Policies
Some organizations may have specific networking policies or compliance regulations that require running database services on custom ports.
3. How to change the default port in PostgreSQL
Overall, changing the port is a simple yet effective way to manage database configurations, improve security, and run multiple instances smoothly. However, there may be times when we need to change this port. For example
- We might run multiple database instances on the same machine.
- We may want to enhance security by using a non-default port.
4. Steps to Change the Default Port in PostgreSQL
Locate the postgresql.conf File
The postgresql.conf file is usually located in the data directory of PostgreSQL installation. The exact location depends on how PostgreSQL was installed, but we can easily find the file by following these steps:
- Linux/Unix Systems: Common paths include /var/lib/pgsql/data or /etc/postgresql/12/main. We can also search for it using the following command:
sudo find / -name postgresql.conf
- Windows Systems: The data directory is usually located in
C:\Program Files\PostgreSQL\version\data
.
Edit the postgresql.conf File
Open the postgresql.conf file in a text editor of your choice:
sudo nano /etc/postgresql/12/main/postgresql.conf
- Windows: Use a text editor like Notepad to open the file.
Look for the following line in the configuration file. The line is commented out by default with a # symbol, meaning the default port (5432) is used
#port = 5432
Uncomment the line and change the port number to your desired value. For example, to change the port to 5433, modify the line as follows then save and close the file.
port = 5433
Restart the PostgreSQL Service
For the changes to take effect, we must restart the PostgreSQL server.
sudo systemctl restart postgresql
Open Services (press Windows + R, type services.msc, and hit enter). Find PostgreSQL in the list, right-click it, and select Restart. Alternatively, we can restart the PostgreSQL service from the command line:
pg_ctl -D /path/to/data/directory restart
- On macOS (using Homebrew):
brew services restart postgresql
Confirm the Port Change
To verify that PostgreSQL is listening on the new port, we can use the following command:
sudo netstat -plnt | grep postgres
We should see an output like this, indicating that PostgreSQL is now listening on the new port (5433 in this example):
tcp 0 0 0.0.0.0:5433 0.0.0.0:* LISTEN 12345/postgres
4. Testing the Connection to the New Port
After changing the port, we need to verify that PostgreSQL is accepting connections on the new port.
Connecting Using psql
We can use the psql command-line tool to connect to our PostgreSQL server. To test the new port, specify the port with the -p option:
psql -h localhost -U postgres -p 5433 -d mydatabase
Replace localhost with the PostgreSQL server's address, postgres with the PostgreSQL username, and mydatabase with the database we want to connect to.
Connecting Using a PostgreSQL GUI
If we are using a PostgreSQL GUI like pgAdmin or DBeaver, we can specify the new port in the connection settings:
In pgAdmin, go to Servers > Properties > Connection tab, and enter the new port number (e.g., 5433).
In DBeaver, when setting up a new connection, specify the port under General > Port.
5. Advanced Configuration: Using Multiple Ports
We can run multiple PostgreSQL instances on the same machine by assigning different ports to each instance. This is particularly useful in development and testing environments where we want to simulate a multi-database setup on a single host.
- Running Multiple Instances: Create a New Data Directory for the second instance (let’s call it pg2)
mkdir /var/lib/pgsql/pg2
- Initialize the new database:
sudo -u postgres initdb -D /var/lib/pgsql/pg2
- Edit the postgresql.conf file in the new data directory (/var/lib/pgsql/pg2):
sudo nano /var/lib/pgsql/pg2/postgresql.conf
- Start the new instance: You can now connect to the new instance on port 5434.
sudo pg_ctl -D /var/lib/pgsql/pg2 start
Automating PostgreSQL Instances
To run multiple PostgreSQL instances automatically, we can set up separate service files for each instance (for example, postgresql-5433 and postgresql-5434), each pointing to its respective configuration and data directory.
6. Troubleshooting Common Issues
Port Already in Use
If we attempt to start PostgreSQL and get an error like:
Could not bind to address '127.0.0.1:5433': Address already in use
This means another process is already using the port. You can use netstat or lsof to identify the process using that port:
sudo netstat -tuln | grep 5433
or
sudo lsof -i :5433
Once you identify the process, you can either stop the service or choose a different port for PostgreSQL.
Connection Refused
If you get a connection refused error after changing the port, make sure:
- The PostgreSQL service has been restarted properly.
- The firewall is not blocking the new port.
- The postgresql.conf file is configured with the correct IP addresses (check the listen_addresses parameter, which should be set to * to allow all addresses or localhost for local connections).
Authentication Errors
After changing the port, make sure we update the connection string or any client configurations to use the new port. For example, in a connection string:
postgresql://user:password@localhost:5433/mydatabase
7. Best Practices
Use Ports Above 1024
When choosing a new port for PostgreSQL, ensure that the port number is above 1024 (the range reserved for system processes and root services). This avoids conflicts with other system processes.
Update Firewall and Security Rules
If we change the port, ensure that firewall rules, security groups, and access control lists (ACLs) are updated to allow traffic on the new port.
Monitor Logs
After changing the port, monitor the PostgreSQL logs (/var/log/postgresql/postgresql.log) for any issues that may arise related to connection or performance.
Conclusion
Changing the default port in PostgreSQL is a simple process that can help improve security, resolve port conflicts, and enable running multiple instances on the same server.
Whether we are working in development, staging, or production environments, customizing the PostgreSQL port is a valuable tool for managing your database configurations.
Similar Reads
How To Change The Default Port 5000 in Svelte?
In Svelte, the application is by default rendered on port 5000 when using the development server. However, you might want to change this port to avoid conflicts with other applications or to fit specific requirements. It can be done through different approaches, such as updating the package.json fil
3 min read
How to Change the Default Port in Spring Boot?
Spring Boot framework provides a default embedded server i.e. the Tomcat server for many configuration properties to run the Spring Boot application. The application runs on the default port which is 8080. As per the application need, we can also change this default port for the embedded server. In
4 min read
Change the Django Default Runserver Port
Changing the default run server port in Django is a simple yet useful adjustment for various development scenarios. By specifying a different port, you can avoid conflicts, manage multiple projects, and test your application in different environments. Remember to adjust your development and testing
3 min read
How to Change Default MySQL/MariaDB Port in Linux?
The default port that the MySQL database server runs under Linux is 3306/TCP. Use the commands below to change the default MySQL/MariaDB Database port in Linux. vi /etc/mysql/mariadb.conf.d/50-server.cnf Search for the line MYSQL, find port under this line, and replace port values accordingly. [mysq
1 min read
How to Create Tables on Heroku Postgresql
Heroku is a Platform as a Service (PaaS) used by developers to deploy their projects with different other requirements provided by the platform like database and all, whereas PostgreSQL is an open-source object-relational database system used for database management in many projects. This article is
3 min read
How to Change FTP Port in Linux?
Files are either uploaded or downloaded to the FTP server. The files are moved from a personal computer to the server when you upload files. The files are moved from the cloud to your personal computer when the files are downloaded. In order to transfer files through FTP, TCP/IP (Transmission Contro
1 min read
How to Change the Port in Jenkins?
Configuring the port in Jenkins is a fundamental aspect of managing the Jenkins server. By default, Jenkins runs on port 8080, but there are scenarios where you may need to change this port to avoid conflicts with other services or security reasons. Understanding how to configure the port in Jenkins
6 min read
How to Configure PostgreSQL in Linux?
Quick Preview to Configure PostgreSQL on Linux:Install PostgreSQL on Linux:On Linux Terminal, execute the command sudo apt-get updateTo install and execute the command, sudo apt install postgresql postgresql-contrib Check the Status of PostgreSQL on Linux:Checking status use command service postgres
5 min read
How to Check Column Types in PostgreSQL?
In PostgreSQL, checking column types is an important aspect of understanding the structure and data stored in a database table. It helps database developers and administrators work effectively by providing a clear picture of the database schema. In this article, we will explore various methods to ch
4 min read
How to Change Port in Flask app
In this article, we will learn to change the port of a Flask application. The default port for the Flask application is 5000. So we can access our application at the below URL. http://127.0.0.1:5000/ We may want to change the port may be because the default port is already occupied. To do that we ju
1 min read