{"id":4480,"date":"2023-05-31T16:09:51","date_gmt":"2023-05-31T16:09:51","guid":{"rendered":"https:\/\/himbap.com\/blog\/?p=4480"},"modified":"2023-05-31T16:09:51","modified_gmt":"2023-05-31T16:09:51","slug":"connecting-azure-functions-with-postgresql","status":"publish","type":"post","link":"https:\/\/himbap.com\/blog\/?p=4480","title":{"rendered":"Connecting Azure Functions with PostgreSQL"},"content":{"rendered":"<p><strong>Introduction<\/strong>:<br \/>\nIn the world of cloud computing, Azure Functions have become a popular choice for developing serverless applications. These lightweight, event-driven functions offer scalability and cost-efficiency, making them an ideal solution for various use cases. Recently we got one such requirement where we need to read PostgreSQL database view and need to update data in Dynamics 365. We used Azure function to connect with PostgreSQL, a powerful open-source relational database management system. In this blog post, we will see sample code to connect with PostgreSQL from Azure Function.<\/p>\n<p><strong>Details<\/strong><br \/>\nOur requirement was to Fetch data from PostgreSQL daily and update Dynamics 365 CE data, so we used Azure Timer Trigger Function to connect data. Below are the high level steps to setup connection<\/p>\n<p>1: Set Up Azure Timer Trigger Function App.<br \/>\n2. We need to Add reference to Npgsql (https:\/\/www.nuget.org\/packages\/Npgsql\/)<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n string connectionstring = string.Empty;\r\n string viewquery = string.Empty;\r\n\r\n string host = Environment.GetEnvironmentVariable(&quot;Host&quot;);\r\n string port = Environment.GetEnvironmentVariable(&quot;Port&quot;);\r\n string database = Environment.GetEnvironmentVariable(&quot;Database&quot;);\r\n string username = Environment.GetEnvironmentVariable(&quot;User&quot;);\r\n string password = Environment.GetEnvironmentVariable(&quot;Password&quot;);\r\n string timeout = Environment.GetEnvironmentVariable(&quot;Timeout&quot;);\r\n\r\n            \/\/prepare connection string\r\n            connectionstring = &quot;Host=&quot; + host + &quot;;Port=&quot; + port + &quot;;Database=&quot; + database + &quot;;Username=&quot; + username + &quot;;Password=&quot; + password + &quot;;Timeout=&quot; + timeout;\r\n\r\n            viewquery = &quot;select * from &lt;ViewName&gt;&quot;;\r\n using (NpgsqlConnection connection = new NpgsqlConnection(connectionstring))\r\n            {\r\n                try\r\n                {\r\n                    logger.LogInformation(&quot;Opening connection to Postgres...&quot;);\r\n                    connection.Open();\r\n                    logger.LogInformation(&quot;Connected to Host:&quot; + host);\r\n                    logger.LogInformation(&quot;Feteching data from &lt;ViewName&gt;&quot;);\r\n\r\n                    using (var command = new NpgsqlCommand(viewquery, connection))\r\n                    {\r\n\r\n                        using (var reader = command.ExecuteReader())\r\n                        {\r\n\t\t\t\t\t\t\/\/PostgreSQLView is class where we have properties that we want to fetch from view\r\n                         List&lt;PostgreSQLView&gt; list = new List&lt;PostgreSQLView&gt;();\r\n\r\n                            while (reader.Read())\r\n                            {\r\n                                PostgreSQLView tbl=new PostgreSQLView();\r\n                                tbl.id= reader.GetInt32(0).ToString();\r\n                                tbl.name = reader.GetString(1).ToString();\r\n                                list.Add(tbl);\r\n                              \r\n                            }\r\n                        }\r\n                    }\r\n\r\n                    connection.Close();\r\n                  }\r\n                catch (Exception ex)\r\n                {\r\n                    logger.LogInformation(ex.Message);\r\n\r\n                }\r\n\r\n\r\n\r\n            }\r\n<\/pre>\n<p>Summary:<br \/>\nOnce we have data from view we can connect to Dynamics 365 and update data based on the identifiers. You can check our earlier blogs for Azure Function<\/p>\n<p><a href=\"https:\/\/himbap.com\/blog\/?p=3689\">Developing Scheduler for Dynamics 365 CE using Azure Function Part 1<\/a><br \/>\n<a href=\"https:\/\/himbap.com\/blog\/?p=3701\">Developing Scheduler for Dynamics 365 CE using Azure Function Part 2<\/a><br \/>\n<a href=\"https:\/\/himbap.com\/blog\/?p=3716\">Using Configuration with Azure Function<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction: In the world of cloud computing, Azure Functions have become a popular choice for developing serverless applications. These lightweight, event-driven functions offer scalability and cost-efficiency, making them an ideal solution for various use cases. Recently we got one such requirement where we need to read PostgreSQL database view and need to update data in Dynamics 365. We used Azure&#8230; <a href=\"https:\/\/himbap.com\/blog\/?p=4480\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[491,402,522],"tags":[764,1038,1040,1039],"_links":{"self":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4480"}],"collection":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4480"}],"version-history":[{"count":1,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4480\/revisions"}],"predecessor-version":[{"id":4481,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4480\/revisions\/4481"}],"wp:attachment":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4480"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4480"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4480"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}