Issues connecting to Azure SQL database - Python

ChrisU 231 Reputation points
2022-05-16T14:08:58.193+00:00

Hi all! So I am having problems connecting to my Azure SQL database. I am trying to do this with python by following the instructions here:
https://learn.microsoft.com/en-us/sql/connect/python/pyodbc/step-3-proof-of-concept-connecting-to-sql-using-pyodbc?view=sql-server-ver15

My code:
202324-image.png
The results when run:
202362-image.png

I have installed pyodbc and confirmed that I have ODBC driver 17 installed:
202348-image.png

I performed a trace on the azure server, which seems to suggest that the company firewall is not an issue (though would seek advice on this):
202325-image.png

Tried pretty much every suggestion here:
https://stackoverflow.com/questions/18860620/cannot-establish-connection-to-sql-server-using-pyodbc-on-windows-7
Including defining the connection string parameters as keyword arguments:
cnxn = pyodbc.connect(driver='{ODBC Driver 17 for SQL Server}', SERVER=server, DATABASE=database, UID=username, PWD=password)

I have set the public access firewall settings for the database within the Azure Hub following the instructions here: IP firewall rules - Azure SQL Database and Azure Synapse Analytics | Microsoft Learn
202315-image.png
My settings:
202359-image.png

I then tried to connect to the database with Visual Studio Code from with the Azure Hub:
202363-image.png
However, I had similar luck:
202310-image.png

Could anyone offer me any advice on what the problem may be and how I can overcome this to connect to my Azure SQL database?

Thanks in advance!

Azure SQL Database
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
{count} vote

Answer accepted by question author
  1. Alberto Morillo 35,401 Reputation points MVP Volunteer Moderator
    2022-05-16T17:19:13.137+00:00

    You need port TCP port 1433 but you also need to know if you are able to resolve the name of the Azure SQL logical server (xxx.database.windows.net) to an IP address. If you ping the name of the Azure SQL logical server you will see the ping will fail but it should also return the current IP address of the server, if that does not happen on your corporate network then you may have problems to connect to Azure SQL, even if you use the IP address, as the IP address changes over time.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. ChrisU 231 Reputation points
    2022-05-16T15:19:21.623+00:00

    Hi Geetha and thanks for your response. I have since investigated this further and by using a phone hotspot connection and it is now working. So it must have been a firewall issue on my companies network. Do you have any idea what ports need to be open on a firewall to allow these sorts of connections to an Azure SQL database?

    1 person found this answer helpful.
    0 comments No comments

Your answer

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