How to Change Connection Port 5432 in PostgreSQL: Step-by-Step Guide

  • Last updated: Nov 07, 2024
  • Views: 28
  • Author: Admin

When configuring a PostgreSQL database server to work in different environments, you may sometimes need to change the connection port other than the default 5432. This may be necessary for a variety of reasons, such as enhancing security, preventing conflicts with other services, or working in specific network configurations. Changing the port in PostgreSQL is a simple procedure that requires configuring a few parameters in the database configuration files and restarting the server.

In this article, we will walk you through the step-by-step process of changing the connection port in PostgreSQL, starting with editing the configuration files and ending with checking that the connection to the database is correct.

The article on the page is based on my many years of experience with this technology and is presented to you as a short instruction, but if you want to get acquainted with it in more detail, then I recommend that you go to the official documentation on PostgreSQL.

 

Contents of the article:

  1. Checking the current port.
  2. Change port.
  3. Checking the result.

 

1. Checking the current port.

By default, the PostgreSQL database uses connection port 5432. We can use two simple methods to check.

The first way is using a sql query:

sql> SELECT name, setting FROM pg_settings WHERE name = 'port';

postgresql change port

 

The second way is using the Linux netstat command:

~$ sudo netstat -plunt |grep postmaster

postgresql change port

As we can see in all the results we have port 5432.


 

2. Change port.

Changing the connection port dynamically will not work because this variable is static. We need to change this parameter in the configuration file postgresql.conf, which is located in the same directory where you have your database installed.

Change the port parameter in the postgresql.conf file. Change the port parameter in the postgresql.conf file.

postgresql change port

Save the file and be sure to restart the database.


 

3. Checking the result.

postgresql change port

When connecting to the database, we now need to additionally specify the connection port, or add a special variable called PGPORT = 5444 to bash_profile.

 

postgresql change port

The postmaster process now also listens on port 5444 instead of 5432.


 

As a result, today we looked at a simple way to quickly and easily change the connection port in PostgreSQL from the standard 5432 to any other that you need in the robot.

 

SIMILAR ARTICLES