Cant access Postgres, getting an error about “/var/run/postgresql/.s.PGSQL.5432” - postgresql

I was running a Django application with Postgres as backend database. It was working fine. All of a sudden, today I saw my database connection is refusing in the production server. So I logged into my server and tried to:
psql
and then it was showing this error:
psql: could not connect to server: No such file or directory
Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
The file listed above does not seem to exist.
I checked if my Postgres is running or not with:
/etc/init.d/postgresql status
and it was returning with SUCCESS message.
postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
Active: active (exited) since Thu 2017-11-16 10:03:41 UTC; 55min ago
Process: 4701 ExecReload=/bin/true (code=exited, status=0/SUCCESS)
Process: 4899 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
Main PID: 4899 (code=exited, status=0/SUCCESS)
Nov 16 10:03:41 median systemd[1]: Starting PostgreSQL RDBMS...
Nov 16 10:03:41 median systemd[1]: Started PostgreSQL RDBMS.
Still, I stopped and started postgres again.
I checked if there any other postgres task running or not with:
ps -ef | grep postgres
And it returned:
root 5210 5117 0 10:21 pts/0 00:00:00 grep --color=auto postgres
So there is no other Postgres.
I even checked if my Postgres accidentally got deleted or not with
dpkg -l | grep postgres
and it returned
ii postgresql 9.5+173ubuntu0.1 all object-relational SQL database (supported version)
ii postgresql-9.5 9.5.10-0ubuntu0.16.04 amd64 object-relational SQL database, version 9.5 server
ii postgresql-client-9.5 9.5.10-0ubuntu0.16.04 amd64 front-end programs for PostgreSQL 9.5
ii postgresql-client-common 173ubuntu0.1 all manager for multiple PostgreSQL client versions
ii postgresql-common 173ubuntu0.1 all PostgreSQL database-cluster manager
ii postgresql-contrib-9.5 9.5.10-0ubuntu0.16.04 amd64 additional facilities for PostgreSQL
So there is no rc instead of ii, which means it has not been uninstalled.
I have tried every other solution in the internet but uninstalling Postgres. Didn't work for me. I might be doing something very silly. I don't want to uninstall and lose my data.

Run the following command to check which port your PostgreSQL server is actually running on:
[root#server ~]# netstat -a | grep .s.PGSQL
unix 2 [ ACC ] STREAM LISTENING 2534417 /var/run/postgresql/.s.PGSQL.5432
I can see from this output that my server is listening on port 5432 and also that the file is created in /var/run/postgresql/.s.PGSQL.5432
You can then update /etc/init.d/postgresql to include the lines
PGPORT=5432
export PGPORT
and update your postgresql.conf file which is located in your data directory and set the port key/value to port=5432
and then restart your PostgreSQL server
[root#server] service postgresql restart

Related

How can I upgrade PostgreSQL from version 11 to version 13?

I'm trying to upgrade PostgreSQL from 11 to 13 on a Debian system, but it fails. I have a single cluster that needs to be upgraded:
$ sudo -u postgres pg_lsclusters
Ver Cluster Port Status Owner Data directory Log file
11 main 5432 online postgres /var/lib/postgresql/11/main /var/log/postgresql/postgresql-11-main.log
Here's what I've tried to upgrade it:
$ sudo -u postgres pg_upgradecluster 11 main
Stopping old cluster...
Warning: stopping the cluster using pg_ctlcluster will mark the systemd unit as failed. Consider using systemctl:
sudo systemctl stop postgresql#11-main
Restarting old cluster with restricted connections...
Notice: extra pg_ctl/postgres options given, bypassing systemctl for start operation
Error: cluster configuration already exists
Error: Could not create target cluster
After this, the system is left in an unusable state:
$ sudo systemctl status postgresql#11-main.service
● postgresql#11-main.service - PostgreSQL Cluster 11-main
Loaded: loaded (/lib/systemd/system/postgresql#.service; enabled-runtime; vendor preset: enabled)
Active: failed (Result: exit-code) since Tue 2022-06-14 06:48:20 CEST; 19s ago
Process: 597 ExecStart=/usr/bin/pg_ctlcluster --skip-systemctl-redirect 11-main start (code=exited, status=0/SUCCE>
Process: 4508 ExecStop=/usr/bin/pg_ctlcluster --skip-systemctl-redirect -m fast 11-main stop (code=exited, status=>
Main PID: 684 (code=exited, status=0/SUCCESS)
CPU: 1.862s
Jun 14 06:47:23 argos systemd[1]: Starting PostgreSQL Cluster 11-main...
Jun 14 06:47:27 argos systemd[1]: Started PostgreSQL Cluster 11-main.
Jun 14 06:48:20 argos postgresql#11-main[4508]: Cluster is not running.
Jun 14 06:48:20 argos systemd[1]: postgresql#11-main.service: Control process exited, code=exited, status=2/INVALIDARG>
Jun 14 06:48:20 argos systemd[1]: postgresql#11-main.service: Failed with result 'exit-code'.
Jun 14 06:48:20 argos systemd[1]: postgresql#11-main.service: Consumed 1.862s CPU time.
$ sudo systemctl start postgresql#11-main.service
Job for postgresql#11-main.service failed because the service did not take the steps required by its unit configuration.
See "systemctl status postgresql#11-main.service" and "journalctl -xe" for details.
Luckily, rebooting the system brought the old cluster back online, but nothing has been upgraded. Why does the upgrade fail? What are "the steps required by its unit configuration"? How can I upgrade PostgreSQL with minimal downtime?
I found the source of my problem: a configuration file owned by the wrong user (root instead of postgres) that could not be removed by the pg_dropcluster command because I ran it as the user postgres.
For future reference, here are the correct steps to upgrade a PostgreSQL cluster from 11 to 13:
Verify the current cluster is the still the old version:
$ pg_lsclusters
Ver Cluster Port Status Owner Data directory Log file
11 main 5432 online postgres /var/lib/postgresql/11/main /var/log/postgresql/postgresql-11-main.log
13 main 5434 down postgres /var/lib/postgresql/13/main /var/log/postgresql/postgresql-13-main.log
Run pg_dropcluster 13 main as user postgres:
$ sudo -u postgres pg_dropcluster 13 main
Warning: systemd was not informed about the removed cluster yet.
Operations like "service postgresql start" might fail. To fix, run:
sudo systemctl daemon-reload
Run the pg_upgradecluster command as user postgres:
$ sudo -u postgres pg_upgradecluster 11 main
Verify that everything works, and that the only online cluster is now 13:
$ pg_lsclusters
Ver Cluster Port Status Owner Data directory Log file
11 main 5434 down postgres /var/lib/postgresql/11/main /var/log/postgresql/postgresql-11-main.log
13 main 5432 online postgres /var/lib/postgresql/13/main /var/log/postgresql/postgresql-13-main.log
Drop the old cluster:
$ sudo -u postgres pg_dropcluster 11 main
Uninstall the previous version of PostgreSQL:
$ sudo apt remove 'postgresql*11'
The Debian packages create a cluster automatically when you install the server package, so get rid of that:
pg_dropcluster 13 main
Then stop the v11 server and try again.

Failed to start PostgreSQL Cluster 10-main when booting

when I try to boot Ubuntu, it never finishes the boot process because it appears the message "Failed to start PostgreSQL Cluster 10-main." I also get the same message with 9.5-main. But lets focus on 10.
When I execute:
systemctl status postgresql#10-main.service
I get the following message:
postgresql#10-main.service - PostgreSQL Cluster 10-main
Loaded: loaded (/lib/systemd/system/postgresql#.service; indirect; vendor preset: enabled)
Active: failed (Result: protocol) since Wed 2020-02-19 17:57:22 CET; 30 min ago
Process: 1602 ExecStart=/usr/bin/pg_ctlcluster --skip-systemctl-redirect 10-main start (code_exited, status=1/FAILURE)
PC_info systemd[1]: Starting PostgreSQL Cluster 10-main...
PC_info postgresql#10-main[1602]: Error: /usr/lib/postgresql/10/bin/pg_ctl /usr/lib/postgresql/10/bin/pg_ctl start -D /var/lib/postgresql/10/main -l /var/log/postgresql/postgresql-19-main.log -s -o -c config_file="/etc/postgresql/10/main/postgresql.conf" exit with status 1:
PC_info systemd[1]: postgresql#10-main.service: Can't open PID file /var/run/postgresql/10-main.pid (yet?) after start: No such file or directory
PC_info systemd[1]: postgresql#10-main.service: Failed with result 'protocol'.
PC_info systemd[1]: Failed to start PostgreSQL Cluster 10-main.
PC_info is information about my computer (user, date..) not relevant
I got this error from one day to an other without touching anything related to Database Servers.
I tried to fix it by my self but nothing worked
Writing the command
service postgresql#10-main start
I get
Job for postgresql#10-main.service failed because the service did not take the steps required by its unit configuration
See "systemctl status postgresql#10-main.service" and "journalctl -xe" for details.
Running this two command I get the message from the beginning.
Anyone has an idea of what is happening? How I can fix it?
I had same issue, I followed below steps,
Error status :
pg_lsclusters
Ver Cluster Port Status Owner Data directory Log file
10 main 5432 down postgres /var/lib/postgresql/10/main
/var/log/postgresql/postgresql-10-main.log
Applied Solution :
sudo chmod 700 -R /var/lib/postgresql/10/main
sudo -i -u postgres
postgres#abc:~$ /usr/lib/postgresql/10/bin/pg_ctl restart -D /var/lib/postgresql/10/main
After Solution status :
pg_lsclusters
Ver Cluster Port Status Owner Data directory Log file
10 main 5432 online postgres /var/lib/postgresql/10/main
/var/log/postgresql/postgresql-10-main.log
as mentioned by #gruentee in comment above,
/usr/lib/postgresql/10/bin/pg_ctl restart -D /var/lib/postgresql/10/main -l /var/log/postgresql/postgresql-10-main.log -s -o '-c config_file="/etc/postgresql/10/main/postgresql.conf"'
started the postgresSql DB. Dont forget to
sudo -i -u postgres
before issuing above command
I was facing the same challenge, I try a lot of methods but none work for me till I try these commands below
sudo apt-get -y install postgresql
sudo systemctl start postgresql#15-main.service
pg_lsclusters
then everything started working fine, but please the version of postgresql i'm using is 15 that's you are seeing 15 in the second command so in your case you substitute it with the version you are using

Armitage 'Connection refused' error in new install of Kali Linux after full upgrade

I installed Kali Linux via VMware and did a full system upgrade:
apt-get update
apt-get upgrade
apt-get full-upgrade
As part of the upgrade postgresql upgraded from v11 to v12. I followed the instructions to finish this part of the upgrade:
pg_dropcluster 12 main --stop
pg_upgradecluster 11 main
pg_dropcluster 11 main
I start postgresql, initialize metasploit, and start Armitage:
/etc/init.d/postgresql start
msfdb init
armitage
The only console output appears unrelated:
Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=on
-Dswing.aatext=true
I do get the popup box with the connection information. I found that I get the "Unexpected end of file from server" if I use 'localhost' as the host, so - per their instructions - I change it to the external IP (in this case 192.168.9.134). I checked metasploit-framework/config/database.yml for
the port and login credentials.
After clicking 'Connect' with this information I get a connection window stating:
Connecting to 192.168.9.134:5432 Connection refused (Connection
refused)
There's also the progress bar that over time will completely fill up (unless I click 'Cancel'). After which nothing happens. As I run the command from the terminal I can see that the process is still running (I don't get my prompt back) but the window disappears and Armitage doesn't actually start. The log file, as verified by pg_lsclusters (/var/log/postgresql/postgresql-12-main.log) doesn't is actually empty.
The link I mentioned before suggests that the problem could either be not enough RAM (I set the VM to have 4gb and free -m shows):
total used free shared buff/cache available
Mem: 3964 803 2677 29 483 2787
Swap: 4093 0 4093
Or that the Metasploit RPC daemon never started (that window does come up the first time, but not subsequent times). I verified that it's running via msfdb status:
● postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/lib/systemd/system/postgresql.service; disabled; vendor preset: disabled)
Active: active (exited) since Fri 2020-02-07 16:06:52 EST; 19min ago
Process: 1753 ExecStart=/bin/true (code=exited, status=0/SUCCESS) Main PID: 1753 (code=exited, status=0/SUCCESS)
Feb 07 16:06:52 kali systemd1: Starting PostgreSQL RDBMS... Feb 07
16:06:52 kali systemd1: Started PostgreSQL RDBMS.
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME postgres
1735 postgres 3u IPv6 32516 0t0 TCP localhost:5432 (LISTEN)
postgres 1735 postgres 4u IPv4 32517 0t0 TCP localhost:5432
(LISTEN)
UID PID PPID C STIME TTY STAT TIME CMD postgres 1735
1 0 16:06 ? Ss 0:00 /usr/lib/postgresql/12/bin/postgres -D
/var/lib/postgresql/12/main -c
config_file=/etc/postgresql/12/main/postgresql.conf
[+] Detected configuration file
(/usr/share/metasploit-framework/config/database.yml)
Also, running regular Metasploit appears to work fine (msfconsole) and loads without error (not sure if there's any output that would be helpful here). I don't use postgresql directly, so I haven't messed with any configuration nor do I have any other applications (that I'm aware of) that use it, so it should be a pretty clean setup (not to mention this is a fresh install of Kali Linux). I'm out of ideas for what to check next. An online search didn't seem to match this problem well. Any thoughts?
Armitage has been deprecated for some time now, as it has not been updated since 2015, and is (to some extent) incompatible with current versions of metasploit.
Although this may not fix your problem, I suggest not using software this much out of date.

Updated Xubuntu, Postgres Stopped Working

I updated my Xubunutu, It was originally 19.04 when I install on this labtop, I think I did one update in the past, now on Xubuntu 19.10.
Postgres was working just prior to the upgrade, I think I may have two versions of postgres running but many of the solutions I've seen don't seem to work.
When I type: service postgresql status
I get
postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor prese
Active: active (exited) since Mon 2019-12-09 07:33:43 EST; 4h 30min ago
Main PID: 1217 (code=exited, status=0/SUCCESS)
Tasks: 0 (limit: 4915)
Memory: 0B
CGroup: /system.slice/postgresql.service
Dec 09 07:33:43 alexmerced-X550EA systemd[1]: Starting PostgreSQL RDBMS...
Dec 09 07:33:43 alexmerced-X550EA systemd[1]: Started PostgreSQL RDBMS.
lines 1-10/10 (END)
when I type: dpkg -l | grep postgres
I get:
ii postgresql 11+204ubuntu0.1 all object-relational SQL database (supported version)
ii postgresql-11 11.5-1 amd64 object-relational SQL database, version 11 server
ii postgresql-client-11 11.5-1 amd64 front-end programs for PostgreSQL 11
ii postgresql-client-common 204ubuntu0.1 all manager for multiple PostgreSQL client versions
ii postgresql-common 204ubuntu0.1 all PostgreSQL database-cluster manager
When I type: pg_lsclusters
I get:
pg_lsclusters
Ver Cluster Port Status Owner Data directory Log file
11 main 5432 down postgres /var/lib/postgresql/11/main /var/log/postgresql/postgresql-11-main.log
Just in case: psql -h /tmp
psql -h /tmp
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
sudo service postgres restart
Failed to restart postgres.service: Unit postgres.service not found.
ps aux |grep postgres
alexmer+ 26272 0.0 0.0 9028 916 pts/2 S+ 12:11 0:00 grep --color=auto postgres
The fix came in by uninstall all the packages from the dpkg results in the OP, then reinstalling with sudo apt-get install postgresql.
now reconfiguring my users

psql : connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

I have installed postgresql and done all the setup and created db and tables yesterday but today morning I got sudden this error
I have also uninstalled and installed postgresql but still same error. And also I have observed that when installed freshly it is working fine but once I shutdown or restart my system I'm getting the below error.
psql:
could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
I have tried all the solutions given in all the mediums but I'm not able to resolve the issue so I'm posting this question again with my outputs.
The status of postgresql
**sudo /etc/init.d/postgresql status**
postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
Active: active (exited) since Wed 2019-10-30 11:40:39 IST; 5min ago
Process: 7666 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
Main PID: 7666 (code=exited, status=0/SUCCESS)
Tasks: 0
Memory: 0B
CPU: 0
CGroup: /system.slice/postgresql.service
When I run the pg_lsclusters
**pg_lsclusters**
Ver Cluster Port Status Owner Data directory Log file
11 main 5432 down postgres /var/lib/postgresql/11/main /var/log/postgresql/postgresql-11-main.log
**dpkg -l | grep postgres**
ii pgdg-keyring 2018.2 all keyring for apt.postgresql.org
ii postgresql 11+207.pgdg16.04+1 all object-relational SQL database (supported version)
ii postgresql-11 11.5-3.pgdg16.04+1 amd64 object-relational SQL database, version 11 server
ii postgresql-client-11 11.5-3.pgdg16.04+1 amd64 front-end programs for PostgreSQL 11
ii postgresql-client-common 207.pgdg16.04+1 all manager for multiple PostgreSQL client versions
ii postgresql-common 207.pgdg16.04+1 all PostgreSQL database-cluster manager
ii postgresql-contrib 11+207.pgdg16.04+1 all additional facilities for PostgreSQL (supported version)
I even tried to restart the postgres still same issue.
**sudo /etc/init.d/postgresql status**
I had exactly this error message, and this is how I resolved it.
In the log file at /var/log/postgresql/postgresql-13-main.log, these lines were present:
2021-09-15 08:26:04.936 UTC [46258] LOG: invalid value for parameter "lc_messages": "en_US.UTF-8"
2021-09-15 08:26:04.936 UTC [46258] LOG: invalid value for parameter "lc_monetary": "en_US.UTF-8"
2021-09-15 08:26:04.937 UTC [46258] LOG: invalid value for parameter "lc_numeric": "en_US.UTF-8"
2021-09-15 08:26:04.937 UTC [46258] LOG: invalid value for parameter "lc_time": "en_US.UTF-8"
2021-09-15 08:26:04.937 UTC [46258] FATAL: configuration file "/etc/postgresql/13/main/postgresql.conf" contains errors
pg_ctl: could not start server
Examine the log output.
This is caused by missing locales, with these possible fixes:
localedef allegedly fixes this according but I had no luck with this.
localedef -i en_US -f UTF-8 en_US.UTF-8
change postgresql.conf and use a locale that does exist (en_GB in my case)
lc_messages = 'en_GB.UTF-8' # locale for system error message strings
lc_monetary = 'en_GB.UTF-8' # locale for monetary formatting
lc_numeric = 'en_GB.UTF-8' # locale for number formatting
lc_time = 'en_GB.UTF-8' # locale for time formatting
post settings
port = 5432 # (change requires restart)
Port was wrong on my PostgreSQL 13 default config (was 5433) and required a change back to the default 5432. I presume this was a result of upgrading from v11 so it gave it a new port number.
After these changes, PostgreSQL needs to restart and it should now function correctly without errors in the log, and the psql command working as expected.
Try this:
sudo -u postgres -i
And then:
initdb --locale $LANG -E UTF8 -D '/var/lib/postgres/data/'
exit
This has worked for me so far