Find the host name and port using PSQL commands

PostgresqlPsql

Postgresql Problem Overview


I have PSQL running, and am trying to get a perl application connecting to the database. Is there a command to find the current port and host that the database is running on?

Postgresql Solutions


Solution 1 - Postgresql

SELECT *
FROM pg_settings
WHERE name = 'port';

Solution 2 - Postgresql

This command will give you postgres port number

 \conninfo

If postgres is running on Linux server, you can also use the following command

sudo netstat -plunt |grep postgres

OR (if it comes as postmaster)

sudo netstat -plunt |grep postmaster

and you will see something similar as this

tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      140/postgres
tcp6       0      0 ::1:5432                :::*                    LISTEN      140/postgres

in this case, port number is 5432 which is also default port number

credits link

Solution 3 - Postgresql

The default PostgreSQL port is 5432. The host that the database is operating on should have been provided by your hosting provider; I'd guess it would be the same host as the web server if one wasn't specified. Typically this would be configured as localhost, assuming your web server and database server are on the same host.

Solution 4 - Postgresql

select inet_server_addr(); gives you the ip address of the server.

Solution 5 - Postgresql

select inet_server_addr( ), inet_server_port( );

Solution 6 - Postgresql

This is non-sql method. Instructions are given on the image itself. Select the server that you want to find the info about and then follow the steps.

enter image description here

Solution 7 - Postgresql

select inet_server_port(); gives you the port of the server.

Solution 8 - Postgresql

The postgresql port is defined in your postgresql.conf file.

For me in Ubuntu 14.04 it is: /etc/postgresql/9.3/main/postgresql.conf

Inside there is a line:

port = 5432

Changing the number there requires restart of postgresql for it to take effect.

Solution 9 - Postgresql

From the terminal you can do:

\conninfo

I would suggest reading a documentation on their exhaustive list of all commands using:

?

Solution 10 - Postgresql

You can use the command in psql \conninfo you will get You are connected to database "your_database" as user "user_name" on host "host_name" at port "port_number".

Solution 11 - Postgresql

From the terminal you can simply do a "postgres list clusters":

pg_lsclusters

It will return Postgres version number, cluster names, ports, status, owner, and the location of your data directories and log file.

Solution 12 - Postgresql

To find the port number you can run this command (assuming you are on localhost)

select setting from pg_settings where name='port';

Solution 13 - Postgresql

SELECT CURRENT_USER usr, :'HOST' host, inet_server_port() port;

This uses psql's built in HOST variable, documented here

And postgres System Information Functions, documented here

Solution 14 - Postgresql

go to the "Terminal" and just type

service postgres status

In the results you can get the port detailsRunning postgres details

In my case it's running on port "5432" (default).
I'm using CentOS 7.Hope this helps.

Solution 15 - Postgresql

service postgresql status

returns: 10/main (port 5432): online

I'm running Ubuntu 18.04

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjamesathaView Question on Stackoverflow
Solution 1 - Postgresqla_horse_with_no_nameView Answer on Stackoverflow
Solution 2 - PostgresqlKhainoView Answer on Stackoverflow
Solution 3 - PostgresqlBrad KochView Answer on Stackoverflow
Solution 4 - PostgresqlKaarelView Answer on Stackoverflow
Solution 5 - PostgresqlgerardwView Answer on Stackoverflow
Solution 6 - PostgresqlbonCodigoView Answer on Stackoverflow
Solution 7 - PostgresqlAndromidaView Answer on Stackoverflow
Solution 8 - PostgresqlEric LeschinskiView Answer on Stackoverflow
Solution 9 - PostgresqlVinayak MishraView Answer on Stackoverflow
Solution 10 - PostgresqlIntraView Answer on Stackoverflow
Solution 11 - PostgresqlmrwonderfulnessView Answer on Stackoverflow
Solution 12 - PostgresqlA-SharabianiView Answer on Stackoverflow
Solution 13 - PostgresqlprofessorcolmView Answer on Stackoverflow
Solution 14 - PostgresqlRahal KanishkaView Answer on Stackoverflow
Solution 15 - PostgresqlMarcos GaligarciaView Answer on Stackoverflow