Jump to content

Configure PostgreSQL Connection in Logic Apps (Standard) with JDBC, call Function with Execute Query


Recommended Posts

Guest Omar_Abu_Arisheh
Posted

mediumvv2px400.png.2ad546a12b992abb72cf4e2d16332795.png*

 

 

 

Many are using relational databases, the JDBC Connector for Logic Apps (Standard) allows to establish a connection with most relational databases and perform actions. We are exploring here, step-by-step, how to create an Azure PostgreSQL, configure the connection with all the requirements, then call a function and use the response in the body of an email to be sent.

 

 

 

Here is a step-by-step for creating the PostgreSQL in Azure, configuring the connection and other requirements, then creating the Logic App (Standard) workflow, add the actions, and test a function, finally parse the response and send it by email.

 

 

 

  • Create a PostgreSQL on Azure, search for PostgreSQL in the search box in Azure Portal, then select “Azure Database for PostgreSQL”, the cost will depend on the VM resources you select.

 

740x355vv2.png.191f3a3b72c0646885c94ec82ca7e062.png

 

 

 

 

 

 

 

  • Once you create the server, browse to the Connection strings and copy the one for JDBC, this will be used later.

 

543x95vv2.png.e40fe363835f24725a97b57d624b4c0a.png

 

 

 

 

 

 

 

  • I downloaded the pgAdmin to be able to create the database, tables, enter some data, and create the function that we will be calling: PostgreSQL: File Browser
  • I changed the below settings, as I was able to connect from pgAdmin using the Certificate, but not from Logic Apps.

 

676x693vv2.png.9aff13db35eddff074f1ac7de1e04b80.png

 

 

 

 

566x365vv2.png.11638fd4691f9cc694b0654d5c9ecfae.png

 

 

 

  • Under Connection, enter the server URL, full username@servername:

 

566x451vv2.png.f11a18e3717bf53a3ffda0893fe502df.png

 

 

 

  • Under parameters, use sslmode “verify-full”, if you disabled the SSL/TLS in PostgreSQL server you can select “disable”.
  • Use the Certificate mentioned earlier if using SSL/TLS.

 

564x417vv2.png.e56264cd1bfd01f8ae0afbc5c1dfba25.png

 

 

 

  • After connecting successfully, you can create Tables, insert data, and create functions:

 

421x699vv2.png.a83845efeb03de06abe1d714f95d359c.png

 

 

 

 

687x387vv2.png.b0365e3e3cdc298ea9ddfb89c6b3b8f8.png

 

 

 

 

694x151vv2.png.fe41981b4e0c6e6d427973b9058a660e.png

 

 

 

  • You can use the portal:

 

676x431vv2.png.1dcb1dabe2f529b55988673cd9c9a7a8.png

 

 

 

  • You can use Microsoft Azure Storage Explorer as well:

 

629x425vv2.png.06b951fb1ca5788adf479f782db100c5.png

 

 

 

 

 

779x215vv2.png.f95ea7516e6882c9173c656a874bb17e.png

 

 

 

  • If you don’t know which storage to upload to, browse to the Logic App Configurations, and find the storage name:

 

721x119vv2.png.f00452cfb4382c792898e07b9c1074f5.png

 

 

 

  • Under the Logic App Configuration, create a new Application setting, this will allow JDBC Connector use the JAR library we uploaded:
  • Name: AzureWebJobsFeatureFlags
  • Value: EnableMultiLanguageWorker

 

692x301vv2.png.1b6bbc8d3c229d6dfaccbc393c6026d2.png

 

 

 

  • We then add a new action, from the Connectors search, under buit-in Connectors, select JDBC, Execute Query action.
  • To create a new connection, use the URL we got earlier from Postgresql Server, enter the username, and password:

 

558x315vv2.png.0d92d373b3bf3cb19731110870d00db8.png

 

 

 

  • You can edit these values later from the Logic App Configuration (App Settings).

  • The URL should look like this: jdbc:postgresql://mypostgresqlserver.postgres.database.azure.com:5432/postgres?user=myuser@mypostgresqlserver&password=mypassword@&sslmode=disable

 

709x305vv2.png.85f40d577333c278ccf687e8d5c7f654.png

 

 

 

  • For the action, you can either start with a simple select statement on one of your tables, or instead of executing a query you can use Get tables action.
  • For our example, we are calling a Function and passing a parameter value of 1, “SELECT demo_function(1)”

 

716x238vv2.png.58c8ce54e02e3ac03cb2aa7e24bb0896.png

 

 

 

  • In pgAdmin, create this Function (assuming we have a table called mydata with two columns, data1 (bigint) and data2(text)):

 

 

 

CREATE or replace FUNCTION demo_function(in mydata1 bigint)

 

RETURNS text AS $mydata2$

 

DECLARE

 

mydata2 text;

 

begin

 

select data2 from mydata where data1 = mydata1 limit 1 into mydata2;

 

RETURN mydata2;

 

end

 

$mydata2$ language plpgsql

 

 

 

  • Fill the query in the action:

 

814x271vv2.png.8ce38e4d192f63ddf5aa3fff9792d0f8.png

 

 

 

  • Save your workflow, and on the workflow overview; execute the trigger, you should have the response as below:

 

578x445vv2.png.b510c7b13f7d24c5777fd0e8c825e101.png

 

 

 

  • We can now use the result from the execute query and send an email for example.
  • Add a Send an email action (Connector: Outlook 365).
  • And to parse the output refer to this reference if needed: Reference guide for expression functions - Azure Logic Apps | Microsoft Learn
  • As the response is inside an array, I can either put the Send an email action in a for-each or just get the first item (this will throw an error if there is no items in the response of the previous action).

 

body('Execute_query_2')[0]?['demo_function']

 

 

 

690x416vv2.png.422be58a19c7ef69f72e1b557d7d1821.png

 

 

 

  • Now will test the workflow again, and receive an email with the data.

 

679x348vv2.png.83878d81c8aa3f1d443125035553e008.png

 

 

 

Thank you :)

 

 

 

*PostgreSQL and JDBC logos are sole property of their respective owners.

 

Continue reading...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...