How to Check and Change Postgresql’s Default Port

Avatar

By squashlabs, Last Updated: October 30, 2023

How to Check and Change Postgresql’s Default Port

tea

PostgreSQL is a useful open-source relational database management system that is widely used for storing and managing data. By default, PostgreSQL listens on port 5432 for incoming connections. However, in some cases, there may be a need to change the default port to a different value.

1. Checking and Changing the PostgreSQL Port

The first step in resolving PostgreSQL port confusion is to check the current port configuration and, if necessary, change it to the desired port.

To check the current port configuration, you can use the following command in the Linux shell:

sudo su postgres -c "psql -c 'SHOW port;'"

This command will display the current port number that PostgreSQL is listening on. If the port number is 5432, but you want to use port 5433, you will need to change the configuration.

To change the PostgreSQL port, follow these steps:

1. Open the PostgreSQL configuration file, which is typically located at /etc/postgresql/{version}/main/postgresql.conf. Replace {version} with the specific version number of PostgreSQL you are using.

2. Locate the line that starts with port = and change the value to the desired port number. For example, if you want to use port 5433, the line should be changed to port = 5433.

3. Save the changes to the configuration file and exit the text editor.

4. Restart the PostgreSQL service to apply the new port configuration. The command to restart the service may vary depending on your operating system and how PostgreSQL is installed. For example, on Ubuntu, you can use the following command:

sudo service postgresql restart

After restarting the PostgreSQL service, it should be listening on the new port number specified in the configuration file.

Related Article: How to Create a Database from the Command Line Using Psql

2. Connecting to PostgreSQL on a Different Port

Once you have changed the PostgreSQL port to the desired value, you need to update your connection settings to reflect the new port number.

If you are using a graphical tool, such as pgAdmin, you can update the connection settings by modifying the port field in the connection properties. Simply enter the new port number (5433 in this case) and save the changes.

If you are connecting to PostgreSQL programmatically, you will need to update the connection string or configuration in your code. The exact steps to do this will depend on the programming language and database library you are using. Here’s an example in Python using the psycopg2 library:

import psycopg2

conn = psycopg2.connect(
    host="localhost",
    port=5433,
    database="your_database",
    user="your_username",
    password="your_password"
)

# Rest of your code...

In this example, we specify the new port number (5433) in the port parameter when establishing the connection.

3. Troubleshooting Port Conflicts

In some cases, you may still encounter issues even after changing the PostgreSQL port. This can happen if there is a conflict with another service or application that is already using the desired port.

To troubleshoot port conflicts, you can use the following steps:

1. Check if the desired port is already in use by running the following command in the Linux shell:

sudo lsof -i :5433

Replace 5433 with the port number you want to check. If the port is in use, the command will display the process ID (PID) of the program using the port.

2. Use the PID obtained from the previous step to identify the program using the port. You can use the following command to get more information about the process:

sudo ps -p 

Replace with the actual process ID.

3. If the program using the port is not essential, you can stop or disable it to free up the port. Alternatively, you can choose a different port number that is not already in use.

4. After resolving any port conflicts, repeat the steps mentioned earlier to change the PostgreSQL port and update your connection settings.

Related Article: How to Restore a Postgresql Backup File Using the Command Line

Tutorial: Managing PostgreSQL Databases with Vacuumdb

Managing PostgreSQL databases efficiently is crucial for optimal database management. This in-depth guide will help you understand and utilize the power of vacuumdb in... read more

How to Create a PostgreSQL Read Only User

Creating a read-only user in PostgreSQL database is an important step in securing your data. This article provides a guide on how to achieve this, covering topics such... read more

How to Check & Change the DB Directory in PostgreSQL

A detailed look at the functionality and application of postgresql-check-db-dir in PostgreSQL databases. This article explores the common queries used in PostgreSQL, how... read more

How to Disable IPv6 in PostgreSQL Databases

Disabling IPv6 in your PostgreSQL database setup is an important step to ensure optimal performance and security. This article provides a step-by-step guide on how to... read more

Step-by-Step Process to Uninstall PostgreSQL on Ubuntu

Uninstalling PostgreSQL from your Ubuntu system can be a process if you follow the step-by-step instructions provided in this article. From preparing for the... read more

Tutorial: Using Navicat for PostgreSQL Database Management

This article provides a detailed guide on using Navicat for PostgreSQL database management. Learn about data modeling, SQL queries, data migration, database... read more