Hey @lucas.pontes
here are the step-by-step instructions on how to access an external Postgres database that is covered by a VPN in Synapse SparkPool notebooks:
- Open the Synapse SparkPool notebook.
- In the notebook, click on the "File" menu and select "Settings".
- In the "Settings" dialog, select the "Connections" tab.
- In the "Connections" tab, click on the "Add" button.
- In the "Add Connection" dialog, select the "Proxy" connection type.
- In the "Proxy" connection type, enter the following information: Host: The hostname or IP address of the proxy server. Port: The port number of the proxy server. Username: The username for the proxy server. Password: The password for the proxy server.
- Click on the "Test" button to test the connection to the proxy server.
- If the connection is successful, click on the "Save" button.
- Open the connection string for the external Postgres database.
- In the connection string, find the following section:
[connection]
host=localhost
port=5432
database=postgres
username=postgres
password=postgres
- Replace the value of the "
host
" property with the hostname or IP address of the proxy server. - Replace the value of the "
port
" property with the port number of the proxy server. - Add the following section to the connection string:
[proxy]
host=localhost
port=8080
username=username
password=password
- Save the connection string.
- In the Synapse SparkPool notebook, create a new cell.
- In the new cell, import the following libraries:
import pyodbc
- In the new cell, create a connection object to the external Postgres database using the connection string you updated in step 13.
conn = pyodbc.connect(connection_string)
- In the new cell, execute a query against the external Postgres database.
cursor = conn.cursor()
cursor.execute("SELECT * FROM table")
rows = cursor.fetchall()
- In the new cell, print the rows returned by the query.
for row in rows:
print(row)
- Run the cell.
If you have followed these steps correctly, you should be able to connect to the external Postgres database from your Synapse SparkPool notebook and execute queries against it.
If you find this helpful please Accept the answer by clicking Accept Answer Button and Upvote.