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?
21 Answers
SELECT *
FROM pg_settings
WHERE name = 'port';
5 Comments
This command will give you postgres port number
\conninfo
If Postgres is running on a 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, the port number is 5432 which is also the default port number
credit: link
2 Comments
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.
5 Comments
psql command, so that implies localhost:5432. Unless someone got clever with env vars/aliases/etc. I don't love my answer a decade later, but solving the x instead of y is perfectly fine and in this case has helped hundreds of people.select inet_server_addr(); gives you the ip address of the server.
3 Comments
$postgres=# select inet_server_addr(); inet_server_addr ------------------ (1 row)select inet_server_port(); gives you the port of the server.
1 Comment
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.
2 Comments
sudo -u postgres psql -c 'SHOW config_file' and also the path is nowadays 14 of course.-p hostport:dockerport part of the docker run commandSELECT 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
1 Comment
HOST will be the directory in which your UNIX domain socket resides, eg /tmp.Because you said you (yourself) have postgresql running, I'll assume:
- you're on Linux,
- have at least one account with superuser privileges and/or can access the postgres role, and
- (just for fun) you need to access both values within a single transaction from within the database itself
/* SQL CODE */
CREATE TEMP TABLE tmp ( hostname text, port bigint ) ON COMMIT DROP;
COPY tmp FROM PROGRAM $pgm$ printf "$HOSTNAME\t$(i=1 && until [[ "$(psql -U postgres -p $i -qt -c "SELECT 'true'" 2>/dev/null | sed -e '$d' | xargs | tr -d "\n")" == "true" ]]; do i=$(($i+1)) && if [ $i == "65535" ]; then break ; fi ; done && echo $i)"$pgm$ ( format 'text', delimiter '\t' );
SELECT host, port FROM tmp;
will give you both, executing the $pgm$-delimited code as a shell script and returning the values to the server-side COPY API's stdin. Unfortunately, this method needs a table target to invoke the server-side shell.
If you need to be able to call without a temp table, i.e. as a function invocation, try implementing the above shell in the plsh language.
Comments
I think PostgreSQL didn't provide an in-built function to get the hostname of server so we might need to write an extension to get the information from server.
I found there is a PostgreSQL extension pg-hostname which can get the hostname from server.
When we have installed the extension we can enable that and query information by inet_server_port & hostname function.
CREATE EXTENSION hostname;
SELECT hostname(),inet_server_port();

