Is postgresql running and where - postgresql

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

Related

Cannot connect to existing database on docker after 2 months

So, as in title I cannot connect to database on docker. I created new container to see where is a problem. On new container everything is working just fine, on old one is a problem. Both of them are created in the same way.
d7289b0bb93f postgres "docker-entrypoint.s…" 24 minutes ago Up 14 minutes 0.0.0.0:5544->5432/tcp, :::5544->5432/tcp
bc4118c3356c postgres "docker-entrypoint.s…" 2 months ago Up 14 minutes 0.0.0.0:5437->5432/tcp, :::5437->5432/tcp
The one on 5437 just randomly stoped. I use psql -h localhost -p 5437 -U postgres and I get following error:
psql: connection to server at "localhost" (::1), port 5437 failed:
server closed the connection unexpectedly
This probably means the server terminated abnormally before or while
processing the request.
When I connect to newly created container psql -h localhost -p 5544 -U postgres everything works just fine.
Also the same with ports:
> root#:/var/lib/docker/containers# netstat -tunlp | grep 5437
tcp 0 0 0.0.0.0:5437 0.0.0.0:* LISTEN 16161/docker-proxy
tcp6 0 0 :::5437 :::* LISTEN 16167/docker-proxy
> root#:/var/lib/docker/containers# netstat -tunlp | grep 5544
tcp 0 0 0.0.0.0:5544 0.0.0.0:* LISTEN 16426/docker-proxy
tcp6 0 0 :::5544 :::* LISTEN 16434/docker-proxy
Found something strange in logs, I do not understand it tho.
PostgreSQL Database directory appears to contain a database; Skipping initialization
2022-05-30 13:08:27.890 GMT [1] LOG: starting PostgreSQL 14.2 (Debian 14.2-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
2022-05-30 13:08:27.891 GMT [1] LOG: listening on IPv4 address "127.0.0.1", port 5432
2022-05-30 13:08:27.891 GMT [1] LOG: could not bind IPv6 address "::1": Cannot assign requested address
2022-05-30 13:08:27.893 GMT [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2022-05-30 13:08:27.899 GMT [26] LOG: database system was shut down at 2022-05-30 13:08:25 GMT
2022-05-30 13:08:27.904 GMT [1] LOG: database system is ready to accept connections
2022-05-30 13:12:43.197 GMT [1] LOG: received fast shutdown request
2022-05-30 13:12:43.198 GMT [1] LOG: aborting any active transactions
2022-05-30 13:12:43.200 GMT [1] LOG: background worker "logical replication launcher" (PID 32) exited with exit code 1
2022-05-30 13:12:43.200 GMT [27] LOG: shutting down
2022-05-30 13:12:43.210 GMT [1] LOG: database system is shut down
So... After I opend container and went to /var/lib/postgresql/data/postgresql.conf to my shock file was empty. I mean nothing inside... I dunno what the heck happend! Recoverd file, all is good as new.

Can't connect to Postgresql with specific external IP

I can connect to my DigitalOcean Ubuntu 20LTS VM instant that has PostgreSQL 14 installed without issue, but I'm trying to make it more secure with only specific IPs that can connect to the database.
I heard the way to do this is to modify the /etc/postgresql/14/main/postgresql.conf file.
When I have this line, I can connect to my database without issue.
listen_addresses='0.0.0.0'
However, if I modify this line with:
listen_addresses='123.123.123.123'
I get this DataGrip error message: [08001] Connection to 111.111.111.111:12345 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
111.111.111.111:12345 is my (fake) VM's IP and port that I already set up.
123.123.123.123 is my (fake) computer's external IP that I get from here or here
Any suggestions? Is there a log I can search from that will give me a better understanding of what is going on?
Also to note, with listen_addresses='0.0.0.0', running ss -ptl gives an output of
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 4096 127.0.0.53%lo:domain 0.0.0.0:*
LISTEN 0 128 0.0.0.0:ssh 0.0.0.0:*
LISTEN 0 244 0.0.0.0:12345 0.0.0.0:*
LISTEN 0 128 [::]:ssh [::]:*
with listen_addresses='123.123.123.123', running ss -ptl gives an output of
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 4096 127.0.0.53%lo:domain 0.0.0.0:*
LISTEN 0 128 0.0.0.0:ssh 0.0.0.0:*
LISTEN 0 128 [::]:ssh [::]:*
Documentation that I used so far:
https://www.postgresql.org/docs/current/runtime-config-connection.html
https://www.postgresql.org/docs/current/auth-pg-hba-conf.html

How can I get the Postgres port

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.

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.

Port 5432 doesn't listen on Raspberry PI

I'm trying to run postgresql 9.4 on my Raspberry PI 3, but without success.
My /etc/postgresql/9.4/main/pg_hba.conf
host all all 10.0.1.0/24 md5
And this on /etc/postgresql/9.4/main/postgresql.conf
listen_addresses = '*' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
port = 5432 # (change requires restart)
max_connections = 100 # (change requires restart)
When I start the service and get the status:
pi#raspberrypi:~ $ sudo service postgresql start
pi#raspberrypi:~ $ sudo service postgresql status
● postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled)
Active: active (exited) since Thu 2017-03-02 22:51:27 UTC; 29s ago
Process: 2840 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
Main PID: 2840 (code=exited, status=0/SUCCESS)
Mar 02 22:51:27 raspberrypi systemd[1]: Started PostgreSQL RDBMS.
I'm getting only ssh port:
pi#raspberrypi:~ $ sudo netstat -anpt | grep LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 577/sshd
tcp6 0 0 :::22 :::* LISTEN 577/sshd
From my mac I try to connect:
❯ psql -h 10.0.1.4 jdmclub_development pguser
psql: could not connect to server: Connection refused
Is the server running on host "10.0.1.4" and accepting
TCP/IP connections on port 5432?
I don't know what I'm doing wrong, can anybody help me?
EDIT
Postgresql logs says:
2017-03-02 23:18:06 UTC [3993-1] LOG: invalid value for parameter "lc_messages": "en_GB.UTF-8"
2017-03-02 23:18:06 UTC [3993-2] LOG: invalid value for parameter "lc_monetary": "en_GB.UTF-8"
2017-03-02 23:18:06 UTC [3993-3] LOG: invalid value for parameter "lc_numeric": "en_GB.UTF-8"
2017-03-02 23:18:06 UTC [3993-4] LOG: invalid value for parameter "lc_time": "en_GB.UTF-8"
2017-03-02 23:18:06 UTC [3993-5] FATAL: configuration file "/etc/postgresql/9.4/main/postgresql.conf" contains errors
To fix it with the next steps:
sudo raspi-config
4 Localisation Options
Select
I1 Change Locale
Select
Check en_GB.UTF-8 UTF8
OK
C.UTF-8
OK
And try to start postgres service again.
This worked for me.