Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server closes connection unexpectedly when connecting to Azure Postgres

I'm trying to connect to Azure Postgresql using psql from my local machine and get the following error:

$ psql "host=username.postgres.database.azure.com port=5432 dbname=postgres user=username@domain password=mypassword sslmode=require"
psql: error: could not connect to server: server closed the connection unexpectedly
    This probably means the server terminated abnormally
    before or while processing the request.

Local Postgres version: 12.1, Azure Postgres database version: 10, macOS Majove.

This issue only recently started after I reinstalled Postgres using Homebrew. I have no issues running $ psql --host=localhost.

I've tried installing postgresql@10 with homebrew so that when I run psql I get the message psql (12.1, server 10.11). But when I attempt to log into Azure Postgres I get the same error as above.

I'm fairly new to Postgres and Azure and would appreciate any suggestions.

Update:

Thanks for the comments and feedback.

I properly installed Postgres 10 with Homebrew and linked it to fix the problem.

$ brew services stop postgresql
$ brew install postgresql@10
$ brew services start postgresql@10
$ brew link --overwrite --force postgresql@10

I can now access the Azure database as expected.

like image 797
alex-b1729 Avatar asked Dec 31 '19 20:12

alex-b1729


People also ask

Which method closes of PostgreSQL connection?

pg_close() closes the non-persistent connection to a PostgreSQL database associated with the given connection instance.

What port is used to communicate with Azure Database postgres?

To access Azure Database for PostgreSQL from your local computer, ensure that the firewall on your network and local computer allow outgoing communication on TCP port 5432.

Can not connect to postgres?

“Could not connect to server: Connection refused” To be sure that PostgreSQL is running, you can also restart it with systemctl restart postgresql. If this does not fix the problem, the most likely cause of this error is that PostgreSQL is not configured to allow TCP/IP connections.


1 Answers

In this case, you could install the PostgreSQL Client for macOS with Homebrew properly then link it as it will symlink all the tools into the /usr/local/bin directory.

To fix this issue, run the following commands:

$ brew services stop postgresql
$ brew install postgresql@10
$ brew services start postgresql@10
$ brew link --overwrite --force postgresql@10

Ref: https://www.compose.com/articles/postgresql-tips-installing-the-postgresql-client/

like image 77
Nancy Xiong Avatar answered Oct 24 '22 14:10

Nancy Xiong