How can I get the Postgres port - postgresql

I just installed Postgres newly and I try to check if its running and on what port, although I know what default port it normally has
these commands are not showing me the port.
service postgresql status
I get this
postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor prese
Active: active (exited) since Tue 2018-09-18 14:17:14 CEST; 22min ago
Process: 2632 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
Main PID: 2632 (code=exited, status=0/SUCCESS)
Tasks: 0
Memory: 0B
CPU: 0
CGroup: /system.slice/postgresql.service
Sep 18 14:17:14 user-HP-Pavilion-TS-15-Notebook-PC systemd[1]: Starting PostgreS
Sep 18 14:17:14 user-HP-Pavilion-TS-15-Notebook-PC systemd[1]: Started PostgreSQ
Sep 18 14:39:19 user-HP-Pavilion-TS-15-Notebook-PC systemd[1]: Started PostgreSQ
And when I just try to simply check process and see what port it shows this after the command
sudo netstat -plunt |grep postgres
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 2592/postgres
tcp 0 0 127.0.0.1:9187 0.0.0.0:* LISTEN 1065/postgres_expor
How can I get the port to show and what is going on?

This answer on serverfault needs you to connect to the machine and no other packages or root user.
Just connect and run the command.
If you can connect to PostgreSQL from the original machine you can see what port it's listening on:
SHOW port;

First, find the “postmaster” process, the parent of all other PostgreSQL processes.
You can get it from the postmaster.pid file in the PostgreSQL data directory if the database is started.
Then you can get the port with lsof. Assuming the process ID is 23521, run
lsof -P -sTCP:LISTEN -i TCP -a -p 23521
That will show you the port where PostgreSQL is listening.

Related

connect to TDengine via restful, but the port is not monitored

When I try to connect TDengine via its restful API but its 6041 port is not monitored.
Following is more detail info.
systemctl status taosd
● taosd.service - TDengine server service
Loaded: loaded (/etc/systemd/system/taosd.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2021-10-20 15:08:33 CST; 48min ago
Process: 16246 ExecStartPre=/usr/local/taos/bin/startPre.sh (code=exited, status=0/SUCCESS)
Main PID: 16257 (taosd)
Tasks: 57
Memory: 30.1M
CGroup: /system.slice/taosd.service
└─16257 /usr/bin/taosd
Oct 20 15:08:33 ecs-29b3 systemd[1]: Starting TDengine server service...
Oct 20 15:08:33 ecs-29b3 systemd[1]: Started TDengine server service.
Oct 20 15:08:33 ecs-29b3 TDengine:[16257]: Starting TDengine service...
Oct 20 15:08:33 ecs-29b3 TDengine:[16257]: Started TDengine service successfully.
netstat -antp|grep 6030
tcp 0 0 0.0.0.0:6030 0.0.0.0:* LISTEN 16257/taosd
netstat -antp|grep 6041
Any suggestion?
you can check whether taosadapter is running with ps -e | grep taosadapter, If there is no taosadapter running, you should start it.

could not create Unix-domain socket in directory "/var/run/postgresql"

I try to uncomment 'unix_socket_directories = '/var/run/postgresql' in postgresql.conf.
But after doing that, when I try to restart postgresql, I receive
Job for postgresql.service failed because the control process exited with error code. See
"systemctl status postgresql.service" and "journalctl -xe" for details.
Looking for journalctl, I see:
LOG: could not bind Unix socket: Address already in use
HINT: Is another postmaster already running on port 5432? If not, remove socket file
"/var/run/postgresql/.s.PGSQL...and retry.
: WARNING: could not create Unix-domain socket in directory "/var/run/postgresql"
Full stack:
Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled;
vendor preset: disabled)
Active: failed (Result: exit-code) since Ср 2020-01-29 13:39:52 MSK;
27s ago
Process: 2937 ExecStop=/usr/bin/pg_ctl stop -D ${PGDATA} -s -m fast
(code=exited, status=1/FAILURE)
Process: 2550 ExecStart=/usr/bin/pg_ctl start -D ${PGDATA} -s -o -p
${PGPORT} -w -t 300 (code=exited, status=0/SUCCESS)
Process: 2544 ExecStartPre=/usr/bin/postgresql-check-db-dir ${PGDATA}
(code=exited, status=0/SUCCESS)
Main PID: 2554 (code=killed, signal=KILL)
plesk.iline.pro systemd[1]: Starting PostgreSQL database server...
plesk.iline.pro pg_ctl[2550]: LOG: could not bind Unix socket:
Address already in use
plesk.iline.pro pg_ctl[2550]: HINT: Is another postmaster already
running on port 5432? If not, remove socket file
"/var/run/postgresql/.s.PGSQL...and retry.
plesk.iline.pro pg_ctl[2550]: WARNING: could not create Unix-
domain socket in directory "/var/run/postgresql"
plesk.iline.pro systemd[1]: Started PostgreSQL database server.
2 plesk.iline.pro systemd[1]: postgresql.service: main process
exited, code=killed, status=9/KILL
plesk.iline.pro pg_ctl[2937]: pg_ctl: could not send stop signal
(PID: 2554): No such process
plesk.iline.pro systemd[1]: postgresql.service: control process
exited, code=exited status=1
plesk.iline.pro systemd[1]: Unit postgresql.service entered failed
state.
plesk.iline.pro systemd[1]: postgresql.service failed.
Hint: Some lines were ellipsized, use -l to show in full.
Likely there is another PostgreSQL server running on the same port.
You should do what PostgreSQL recommends:
ls -l /var/run/postgresql/.s.PGSQL.5432
There should be a socket file present.
Then, as user root, see if there is a PostgreSQL process listening on the port:
sudo fuser /var/run/postgresql/.s.PGSQL.5432
If there is a result, there is really another PostgreSQL server running on port 5432. Either stop that server or choose a different port for your cluster.
If there is no result, the socket may be left over. Remove it and try again.

could not bind IPv4 socket: Permission denied

I am trying to set up a new instance of PostgreSQL 9.6 on a machine. I have tested it on another machine and its working fine on that machine. But the same process is not working on new machine. Below are the steps I am using
created a new data directory with below command
/opt/rh/rh-postgresql96/root/bin/initdb -D /var/lib/pgsql/9.6/data/
created a service file /etc/systemd/system/rh-postgresql96-inst2.service with below content
.include /lib/systemd/system/rh-postgresql96-postgresql.service
[Service]
Environment=PGDATA=/var/lib/pgsql/9.6/data/
Environment=PGPORT=5433
User=postgres
Group=root
registered service using command systemctl enable rh-postgresql96-inst2
now using command systemctl start rh-postgresql96-inst2 to start service.
All these steps are working fine on one machine but not on the 2nd one.
I am getting below error while starting service on the 2nd machine
rh-postgresql96-inst2.service - PostgreSQL database server
Loaded: loaded (/etc/systemd/system/rh-postgresql96-inst2.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Mon 2018-06-18 09:59:01 UTC; 10s ago
Process: 7552 ExecStart=/opt/rh/rh-postgresql96/root/usr/libexec/postgresql-ctl start -D ${PGDATA} -s -w -t ${PGSTARTTIMEOUT} (code=exited, status=1/FAILURE)
Process: 7550 ExecStartPre=/opt/rh/rh-postgresql96/root/usr/libexec/postgresql-check-db-dir %N (code=exited, status=0/SUCCESS)
HINT: Is another postmaster already running on port 5433? If not, wait a few seconds and retry.
LOG: could not bind IPv4 socket: Permission denied
HINT: Is another postmaster already running on port 5433? If not, wait a few seconds and retry.
WARNING: could not create listen socket for "localhost"
FATAL: could not create any TCP/IP sockets
LOG: database system is shut down
systemd[1]: rh-postgresql96-inst2.service: control process exited, code=exited status=1
systemd[1]: Failed to start PostgreSQL database server.
systemd[1]: Unit rh-postgresql96-inst2.service entered failed state.
systemd[1]: rh-postgresql96-inst2.service failed.
However, I am able to start service using pg_ctl.
Also, I have checked with netstat, lsof command to check if any other postgresql instance is running on port 5433 but its not the case.
Infact i tried 5431, 5434 ports also but server is not starting up
Instead of turning of SELinux you should allow postgres to bind to port 5433 in SELinux.
There is a port parameter postgresql_port_t which by default has port 5432 and 9898.
semanage port -l | grep post
postgresql_port_t tcp 5433, 9898
What you could do is simply add port 5433 to this list.
semanage port -a -t postgresql_port_t 5433 -p tcp
semanage port -l | grep post
postgresql_port_t tcp 5433, 5432, 9898
After that you can start your postgres server listening on port 5433
systemctl enable rh-postgresql96-postgresql
systemctl start rh-postgresql96-postgresql
netstat -tulpn
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 2847/postgres
tcp 0 0 127.0.0.1:5433 0.0.0.0:* LISTEN 2775/postgres
There is also a handy tool called audit2allow to help debug selinux problems.
audit2allow -m whatiswrong < /var/log/audit/audit.log > /root/showme.te
The file showme.te show you why SELinux is not allowing the service to do what you need.
You should not turn off SELinux just because it's hard to understand or if you don't know how it works. Instead you should study it :)
I reccomend this lecture from the Red Hat Summit https://www.redhat.com/en/about/videos/summit-2018-security-enhanced-linux-mere-mortals
This issue was related to SELinux.
When I run command sestatus on both machines, output was a little bit different.
One server had Current mode: permissive and 2nd one had Current mode: enforcing.
So I changed the current mode to permissive on the 2nd machine using command setenforce 0.
and it resolved the permission related issue. Now I am able to start 2nd instance.

Is postgresql running and where

I am really lost on this one. My Django application used to work and then suddenly stopped working, when I start the server I get this
django.db.utils.OperationalError: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
Fair enough, so now I want to check if postgres is running and on what port. This is where I am lost as I can't find it running. The following suggests that it is:
service postgresql status
postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
Active: active (exited) since Thu 2017-11-16 13:29:37 GMT; 6s ago
Process: 7023 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
Main PID: 7023 (code=exited, status=0/SUCCESS)
This gives me some information
ps aux | grep -i postgres
myuser+ 7985 0.0 0.0 14228 976 pts/17 S+ 13:38 0:00 grep --color=auto -i postgres
But I am unclear what it means, this gives me port details
sudo netstat -plunt
For example
roto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:139 0.0.0.0:* LISTEN 1381/smbd
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 791/redis-server 0.
tcp 0 0 127.0.0.1:63342 0.0.0.0:* LISTEN 2415/java
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1211/nginx.conf
tcp 0 0 127.0.1.1:53 0.0.0.0:* LISTEN 841/dnsmasq
The config file here: /etc/postgresql/9.5/main/postgresql.conf
Says that Postgres should run on 5432
My summary is that Postgres isn't running, but I am unsure where else to look to see any more information. Can anyone help?
Thanks
Grant

'service postgresql start' fails to start postgres service on Fedora

Newcomer to postgres here!
I edited pg_hba.conf as mentioned here , but when I try to restart postgresql service, the attempt fails. Below is the command line output with all the information I could gather.
[root#arunpc modules]# service postgresql restart
Redirecting to /bin/systemctl restart postgresql.service
Job failed. See system logs and 'systemctl status' for details.
[root#arunpc modules]# systemctl status postgresql.service
postgresql.service - PostgreSQL database server
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled)
Active: failed since Sun, 08 Apr 2012 21:29:06 +0530; 14s ago
Process: 12228 ExecStop=/usr/bin/pg_ctl stop -D ${PGDATA} -s -m fast (code=exited, status=0/SUCCESS)
Process: 12677 ExecStart=/usr/bin/pg_ctl start -D ${PGDATA} -s -o -p ${PGPORT} -w -t 300 (code=exited, status=1/FAILURE)
Process: 12672 ExecStartPre=/usr/bin/postgresql-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
Main PID: 12184 (code=exited, status=0/SUCCESS)
CGroup: name=systemd:/system/postgresql.service
[root#arunpc modules]# tail /var/log/messages
....
Apr 8 21:29:06 arunpc systemd[1]: postgresql.service: control process exited, code=exited status=1
Apr 8 21:29:06 arunpc systemd[1]: Unit postgresql.service entered failed state.
Apr 8 21:29:06 arunpc pg_ctl[12677]: pg_ctl: could not start server
Apr 8 21:29:06 arunpc pg_ctl[12677]: Examine the log output.
FWIW, here is the configuration file (pg_hba.conf) used:
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all postgres ident sameuser
local all all ident sameuser
# IPv4 local connections:
host all all 127.0.0.1 password
# IPv6 local connections:
host all all ::1 password
What could be the error here? It used to work fine before I made the edit (and since this was a development machine, I brilliantly didn't make any backup).
I would also like to get a more detailed log output. The log message in /var/log/messages file does ask me to "Examine the log output" - which log output would this be? What other troubleshooting steps can I take?
Many thanks in advance!
Depending on your startup script, it might redirect the postmaster's output to a file. This is usually server.log in the PGDATA directory. Things I'd try:
Comment out everything in pg_hba.conf and retry. If the problem is a syntax error in that file, then commenting out the offending line will allow the server to start and then you'll be able to uncomment one at a time until you find the error.
Start postmaster directly from the shell without sending it to the background. Just run postmaster -D <pgdata dir> and it should spew some more helpful logs.