Bootstrap FreeKB - Postgres (SQL) - List Port using the psql command
Postgres (SQL) - List Port using the psql command

Updated:   |  Postgres (SQL) articles

The psql command with a SQL select statement can be used to list the Postgres port. By default, Postgres listens on port 5432 and the port directive in the postgresql.conf file is commented out, like this.

If you want to use some other port, you can update the port directive in the postgresql.conf file to have the port you want to use and then restart postgres for this change to take effect.

#port = 5432

 

If using the default posgres user account, the following command should return the port being used.

sudo -u postgres psql --expanded --command "select * from pg_settings where name = 'port'"

 

If you have created a user account, the -u or --username option and --password flag can be used.

psql --username johndoe --password --expanded --command "select * from pg_settings where name = 'port'"

 

You could setup passwordless authentication using the hidden .pgpass file and then connect without the --password flag. However, in this scenario, the hidden .pgpass file will need to include the port, so this isn't a great option since it will fail if the port in the hidden .pgpass file does not match the port being used.

psql --username johndoe --dbname mydb --expanded --command "select * from pg_settings where name = 'port'"

 

Something like this should be returned

-[ RECORD 1 ]----------------------------------------------------
name       | port
setting    | 5432
unit       |
category   | Connections and Authentication / Connection Settings
short_desc | Sets the TCP port the server listens on.
extra_desc |
context    | postmaster
vartype    | integer
source     | command line
min_val    | 1
max_val    | 65535
enumvals   |
boot_val   | 5432
reset_val  | 5432
sourcefile |
sourceline |

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 6f1083 in the box below so that we can be sure you are a human.