I've been trying to run this batch file that goes through the Postgre DB Server and run two different sql files, as shown below:
set PGPASSWORD=blah
cls
@echo on
"C:\Progra~1\pgAdmin III\1.16\psql" -d [db name] -h [server name] -p 5432 -U postgres -f C:\query1.sql
"C:\Progra~1\pgAdmin III\1.16\psql" -d [db name] -h [server name] -p 5432 -U postgres -f C:\query2.sqlBut the issue comes that sometimes I will get the following error for either the command for query1 or query2:
psql: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.This only happens sometimes, so I'm not entirely sure why it is happening. Can someone explain why this is the case and if there's a solution to this problem. Thanks!
Update: I also get the same error SOMETIMES when trying to open the remote server in the actual Postgre application: "An error has occured: "server closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request."
I also get this Guru Hint thing right after I click out of the error popup:
Database encoding The database VA-trac is created to store data using the SQL_ASCII encoding. This encoding is defined for 7 bit characters only; the meaning of characters with the 8th bit set (non-ASCII characters 127-255) is not defined. Consequently, it is not possible for the server to convert the data to other encodings. If you're storing non-ASCII data in the database, you're strongly encouraged to use a proper database encoding representing your locale character set to take benefit from the automatic conversion to different client encodings when needed. If you store non-ASCII data in an SQL_ASCII database, you may encounter weird characters written to or read from the database, caused by code conversion problems. This may cause you a lot of headache when accessing the database using different client programs and drivers. For most installations, Unicode (UTF8) encoding will provide the most flexible capabilities.
Regardless, the server still opens up afterward and I'm able to access the database from that point on.
614 Answers
Leaving this here for info,
This error can also be caused if PostgreSQL server is on another machine and is not listening on external interfaces.
To debug this specific problem, you can follow theses steps:
- Look at your postgresql.conf,
sudo vim /etc/postgresql/9.3/main/postgresql.conf - Add this line:
listen_addresses = '*' - Restart the service
sudo /etc/init.d/postgresql restart
(Note, the commands above are for ubuntu. Other linux distro or OS may have different path to theses files)
Note: using '*' for listening addresses will listen on all interfaces. If you do '0.0.0.0' then it'll listen for all ipv4 and if you do '::' then it'll listen for all ipv6.
It turns out it is because there was a mismatch between the postgre SQL version between my local and the server, installing the same version of PostgreSQL in my computer fixed the issue. Thanks!
6In my case, it was because I set up the IP configuration wrongly in pg_hba.conf, that sits inside data folder in Windows.
# IPv4 local connections:
host all all 127.0.0.1/32 md5
host all all 192.168.1.0/24 md5I mistakenly entered (copied-pasted :-) ) 192.168.0.0 instead of 192.168.1.0.
2In my case I was making an connection through pgAdmin with ssh tunneling and set to host field ip address but it was necessary to set localhost
this is an old post but...
just surprised that nobody talk about pg_hba file as it can be a good reason to get this error code.
Check here for those who forgot to configure it:
In my case, i'm using Postgresql 9.2.24 and solution was this (pg_hba.conf):
host all all 0.0.0.0/0 trustFor remote connections use trust. Combined with (as mentioned above)
listen_addresses = '*' 3 If your Postgres was working and suddenly you encountered with this error, my problem was resolved just by restarting Postgres service or container.
0If you are using Docker make sure you are not using the same port in another service, in my case i was mistakenly using the same port for both PostgreSQL and Redis.
In my case I was trying use ssh tunnel connection through pgAdmin4 and before that set AllowTcpForwarding no in server's /etc/ssh/sshd_config. But it should be AllowTcpForwarding yes.
If you are connection through a SSH tunnel, this could mean you did a wrong port redirect on the target machine, check the log of the sshd on your jumpbox to see if this is the error.
Solved by setting a password for the user first.
In terminal
sudo -u <username> psql
ALTER USER <username> PASSWORD 'SetPassword';
# ALTER ROLE
\qIn pgAdmin
**Connection**
Host name/address: 127.0.0.1
Port: 5432
Maintenance database: postgres
username: postgres
password: XXXXXX In my case my postgres rds instance was stopped...
In my case it was a discrepancy between postgresql.conf and pg_hba.conf
pg_hba.conf had a hostssl rule
hostssl all all 0.0.0.0/0 md5and
postgresql.conf had
ssl = off I have the same problem in my Postgres.
As it's only happenning to one of my Django models save() method (a huge one), I blame the size of the record being inserted.