@Anuj, Singh (Cognizant) - Thank for the question and using MS Q&A platform.To connect to Azure Database for PostgreSQL using Azure Databricks you can use JDBC driver and PostgreSQL connector without installing any libraries on the Azure Databricks clusters.
For more details, refer to Query PostgreSQL with Azure Databricks.
The sample queries to connect to Azure Database for PostgreSQL using Azure Databricks using JDBC driver:
driver = "org.postgresql.Driver"
database_host = "<database-host-url>"
database_port = "5432" # update if you use a non-default port
database_name = "<database-name>"
table = "<table-name>"
user = "<username>"
password = "<password>"
url = f"jdbc:postgresql://{database_host}:{database_port}/{database_name}"
remote_table = (spark.read
.format("jdbc")
.option("driver", driver)
.option("url", url)
.option("dbtable", table)
.option("user", user)
.option("password", password)
.load()
)
Here is the screenshot of the successful connection:
The sample queries to connect to Azure Database for PostgreSQL using Azure Databricks using PostgreSQL connector:
remote_table = (spark.read
.format("postgresql")
.option("dbtable", "schema_name.table_name") # if schema_name not provided, default to "public".
.option("host", "database_hostname")
.option("port", "5432") # Optional - will use default port 5432 if not specified.
.option("database", "database_name")
.option("user", "username")
.option("password", "password")
.load()
)
Here is the screenshot of the successful connection:
How to run stored procedure in Azure Database for PostgreSQL using Azure Databricks Notebook?
Once you are able to successfully be connected to Azure Database for PostgreSQL using Azure Databricks you can simply run the stored procedure without any issue.
For more details, refer to Query databases using JDBC.
Hope this helps. Do let us know if you any further queries.
If this answers your query, do click Accept Answer
and Yes
for was this answer helpful. And, if you have any further query do let us know.