cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to run stored procedure in Azure Database for PostgreSQL using Azure Databricks Notebook

singhanuj2803
New Contributor III

We have Stored Procedure available in Azure Database for PostgreSQL and we want to call or run or execute the postgreSQL stored procedures in Azure Databricks through Notebook

We are attempting to run PostgreSQL stored procedures, through Azure Databricks notebook and would need help how to execute a stored procedure. I would need clarity in below points

  1. What are the required libraries that needs to be installed in Databricks cluster (if any)?
  2. How to connect with Azure Database for PostgreSQL using ODBC?
  3. How to Execute PostgreSQL stored procedure in Databricks?

I am referring to below link,

https://nachiketrajput44.medium.com/how-to-run-stored-procedure-in-azure-data-warehouse-using-databr...

Let me know if anyone has step-by-step approach how to solve this issue.

1 REPLY 1

Alberto_Umana
Databricks Employee
Databricks Employee

To execute a PostgreSQL stored procedure from an Azure Databricks notebook, you need to follow these steps:

  1. Required Libraries:
    • You need to install the psycopg2 library, which is a PostgreSQL adapter for Python. This can be done using the %pip install psycopg2 command in your Databricks notebook.
  2. Connecting to Azure Database for PostgreSQL using ODBC:
    • You can use the psycopg2 library to establish a connection to your Azure Database for PostgreSQL. Here is an example of how to set up the connection:

import psycopg2

 

conn = psycopg2.connect(

    host="your_postgresql_host",

    database="your_database_name",

    user="your_username",

    password="your_password"

)

 

Executing the PostgreSQL Stored Procedure:

 

  • Once the connection is established, you can execute the stored procedure using a cursor object. Here is an example:

 

cur = conn.cursor()

cur.execute("CALL your_stored_procedure_name()")

conn.commit()

cur.close()

conn.close()

 

Make sure to replace "your_postgresql_host", "your_database_name", "your_username", "your_password", and "your_stored_procedure_name" with your actual PostgreSQL host, database name, username, password, and stored procedure name respectively.

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group