fail to connect to Database for PostgreSQL - Single Server with python

maxianren 21 Reputation points
2021-12-01T14:39:29.86+00:00

i am trying to connect to Database for PostgreSQL - Single Server using python following this instruction
https://learn.microsoft.com/en-us/azure/postgresql/connect-python

my configuration is:
server location: West Europe (i also tried US East, the result is the same)
PostgreSQL version: 11
SSL enforce status: ENABLED

python version: 3.8.8
psycopg2 version: 2.9.2 (dt dec pq3 ext lo64)

however, it keeps return errors:

OperationalError: connection to server at "db-oppo-dlv.postgres.database.azure.com" (xx.xx.xxx.xxx), port xxxx failed: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.*

i get this error even if i open all the ip address in firewall. i think my credential and server information is correct, so what the problem could be? thank you

Azure Database for PostgreSQL
{count} votes

Accepted answer
  1. GeethaThatipatri-MSFT 29,542 Reputation points Microsoft Employee Moderator
    2021-12-04T15:40:37.673+00:00

    Hi, @maxianren Good to see that the above code worked for you.

    To connect from Azure studio you need to select the Connection type as PostgreSQL and your database name. make sure you add your client IP from the portal before selecting the connection type.

    154977-image.png

    Regards
    Geetha

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. GeethaThatipatri-MSFT 29,542 Reputation points Microsoft Employee Moderator
    2021-12-04T00:51:27.97+00:00

    Hi, maxianren-3145 You can also give it a try by commenting on the below code and follow as mentioned by directly connecting and see if it works, however, make sure client IP is added, public IP is not required.
    Also, I want to check if you tried with Azure studio to check the connection is successful.

    Regards
    Geetha

    154963-image.png

    1 person found this answer helpful.

  2. BlaneGrann-1748 1 Reputation point
    2022-06-25T04:19:25.707+00:00

    I had the same issue. It turns out I was using the wrong string for dbname. The error message was not helpful for debugging my mistake.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.