{"id":4047,"date":"2014-11-17T00:03:12","date_gmt":"2014-11-17T00:03:12","guid":{"rendered":"http:\/\/www.jitendrazaa.com\/blog\/?p=4047"},"modified":"2014-11-17T00:15:37","modified_gmt":"2014-11-17T00:15:37","slug":"how-to-use-postgresql-in-nodejs","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/webtech\/how-to-use-postgresql-in-nodejs\/","title":{"rendered":"How to use PostgreSQL in Nodejs"},"content":{"rendered":"<p style=\"text-align: justify;\">I have written many articles on <a title=\"Node.js articles on JitendraZaa.com\" href=\"https:\/\/www.jitendrazaa.com\/blog\/tag\/nodejs\/\">Node.js<\/a> and <a title=\"Angularjs Posts\" href=\"https:\/\/www.jitendrazaa.com\/blog\/tag\/angularjs\/\">AngularJs<\/a>. In this article, I will demonstrate how to use PostgreSQL with Node.js and deploy on <a title=\"Heroku Articles\" href=\"https:\/\/www.jitendrazaa.com\/blog\/tag\/heroku\/\">Heroku<\/a>. I think you already smelled that this is one of starting article which will finally merged with Salesforce to create some cool application.<\/p>\n<p style=\"text-align: justify;\">Assuming you already have some background knowledge on <a title=\"Node.js articles on JitendraZaa.com\" href=\"https:\/\/www.jitendrazaa.com\/blog\/tag\/nodejs\/\">Node.js<\/a>\u00a0and <a title=\"Heroku Articles\" href=\"https:\/\/www.jitendrazaa.com\/blog\/tag\/heroku\/\">Heroku<\/a>, If not please visit previous articles of this blog or my <a title=\"Jitendra Zaa on Slideshare\" href=\"http:\/\/www.slideshare.net\/jitendrazaa\">Slideshare <\/a>account. Using Angularjs is totally optional and you can use library like\u00a0<a title=\"JQuery articles on JitendraZaa.com\" href=\"https:\/\/www.jitendrazaa.com\/blog\/tag\/jquery\/\">JQuery <\/a>or plain <a title=\"JavaScript on JitendraZaa.com\" href=\"https:\/\/www.jitendrazaa.com\/blog\/tag\/javascript\/\">JavaScript<\/a>.<\/p>\n<p style=\"text-align: justify;\">To connect PostgreSQL, we need &#8220;<a title=\"PostgreSQL client - pure javascript &amp; libpq with the same API\" href=\"https:\/\/www.npmjs.org\/package\/pg\">pg<\/a>&#8221; module.<\/p>\n<figure id=\"attachment_4055\" aria-describedby=\"caption-attachment-4055\" style=\"width: 686px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2014\/11\/PostgreSQL-in-Node.js.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4055\" src=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2014\/11\/PostgreSQL-in-Node.js.png?resize=686%2C589&#038;ssl=1\" alt=\"PostgreSQL in Node.js\" width=\"686\" height=\"589\" srcset=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2014\/11\/PostgreSQL-in-Node.js.png?w=686&amp;ssl=1 686w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2014\/11\/PostgreSQL-in-Node.js.png?resize=300%2C257&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2014\/11\/PostgreSQL-in-Node.js.png?resize=624%2C535&amp;ssl=1 624w\" sizes=\"auto, (max-width: 686px) 100vw, 686px\" \/><\/a><figcaption id=\"caption-attachment-4055\" class=\"wp-caption-text\">PostgreSQL in Node.js<\/figcaption><\/figure>\n<p style=\"text-align: justify;\"><!--more-->File -1 is code snippet of &#8220;package.json&#8221;. After creating this file, you will need to run command &#8220;<span style=\"text-decoration: underline;\">npm install<\/span>&#8220;. This command will install all the required dependencies needed for this application.<\/p>\n<p style=\"text-align: justify;\">File 2, shows how different CRUD operations is performed in &#8220;pg&#8221; module of node.js. If you notice this file ; we are using &#8220;<span style=\"text-decoration: underline;\"><span class=\"nx\">process<\/span><span class=\"p\">.<\/span><span class=\"nx\">env<\/span><span class=\"p\">.<\/span><span class=\"nx\">DATABASE_URL<\/span><\/span>&#8220;. This variable will store Database string needed to connect PostgreSQL in heroku, this will be automatically available once you install &#8220;<a title=\"Reliable and powerful database-as-a-service based on PostgreSQL.\" href=\"https:\/\/addons.heroku.com\/heroku-postgresql\">PostgreSQL<\/a>&#8221; add-on in your Heroku application. If this variable is not available (like on local system) then we are using actual connection string of <a title=\"Download PostgreSQL\" href=\"http:\/\/www.postgresql.org\/download\/\">PostgreSQL installed on system<\/a>. you will need to replace this connection string with your credentials.<\/p>\n<p style=\"text-align: justify;\">If you want to create Table schema, you need to\u00a0hit &#8220;\/db\/createTable&#8221; URL and if you want to delete table, hit &#8220;\/db\/dropTable&#8221; URL.<\/p>\n<p style=\"text-align: justify;\">File 3 is Node.js starting file, which actually routes all requests. For code modularity, I\u00a0have separated database operations related code in &#8220;File 2&#8221; and importing in this file.<\/p>\n<p style=\"text-align: justify;\">File 4, is landing page which calls CRUD functions using ajax with the help of Angularjs.<\/p>\n<p style=\"text-align: justify;\">File 5 &#8211; &#8220;App.json&#8221; which is important for &#8220;Heroku 1&#8221; button to work in Github. You can ignore this file. This file basically is used by Heroku to directly install this application on Heroku\u00a0on single click and known as &#8220;<a title=\"Heroku Button\" href=\"https:\/\/blog.heroku.com\/archives\/2014\/8\/7\/heroku-button\">Heroku Button<\/a>&#8220;.<\/p>\n<p>You can find <a title=\"This application demonstrates database operations using PostgreSQL as a database, Heroku as platform, Node.js as language and Angularjs as javascript based MVC\" href=\"https:\/\/github.com\/JitendraZaa\/node-postgres\">complete code here<\/a> on <a title=\"Jitendra Zaa Git repository\" href=\"https:\/\/github.com\/JitendraZaa\">my Git repository<\/a> and <a title=\"database operations using PostgreSQL as a database, Heroku as platform, Node.js as language and Angularjs as javascript based MVC\" href=\"https:\/\/node-postgres.herokuapp.com\/\">working demo here<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<p><script src=\"https:\/\/gist.github.com\/JitendraZaa\/c0d5f17d024e29ec6b8e.js\"><\/script><\/p>\n<p>Please provide your feedback here, See you in next article.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have written many articles on Node.js and AngularJs. In this article, I will demonstrate how to use PostgreSQL with Node.js and deploy on Heroku. I think you already smelled that this is one of starting article which will finally merged with Salesforce to create some cool application. Assuming you already have some background knowledge [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[266,19],"tags":[35,108,342,267,277,276],"class_list":["post-4047","post","type-post","status-publish","format-standard","hentry","category-node-js","category-webtech","tag-angularjs","tag-git","tag-heroku","tag-nodejs","tag-pg","tag-postgresql"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":3951,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/creating-salesforce-query-builder-in-node-js-and-angularjs-using-salesforce-rest-api-and-oauth2\/","url_meta":{"origin":4047,"position":0},"title":"Creating Salesforce Query Builder in Node.js and AngularJs using Salesforce REST API and OAuth2 with Heroku Button","author":"Jitendra","date":"August 18, 2014","format":false,"excerpt":"In this post, we will be creating a\u00a0Salesforce Query (SOQL) Builder.\u00a0It uses Google's Angularjs MVC Javascript library and REST API provided by Salesforce. For Authentication, it uses OAuth2. Node.js is used as a primary language and hosted on Heroku. If you want to directly deploy to your Heroku Account, You\u2026","rel":"","context":"In &quot;HTML&quot;","block_context":{"text":"HTML","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/webtech\/web\/"},"img":{"alt_text":"SOQL Builder Using Node.js AngularJs Salesforce REST API on Heroku","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2014\/08\/SOQL-Builder-Using-Node.js-AngularJs-Salesforce-REST-API-on-Heroku-1024x504.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2014\/08\/SOQL-Builder-Using-Node.js-AngularJs-Salesforce-REST-API-on-Heroku-1024x504.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2014\/08\/SOQL-Builder-Using-Node.js-AngularJs-Salesforce-REST-API-on-Heroku-1024x504.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":3937,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/heroku\/create-node-js-application-using-eclipse-and-deploy-to-heroku-video-tutorial\/","url_meta":{"origin":4047,"position":1},"title":"Create Node.js Application using Eclipse and deploy to Heroku &#8211; Video Tutorial","author":"Jitendra","date":"July 27, 2014","format":false,"excerpt":"In Below video, I have explained how to create basic Node.js application in Eclipse with help of Egit and Heroku plugin and deploy to Heroku. Please read below two articles, to get familiar of Plugins and how to setup : Salesforce + Git + Eclipse + EGIT = Better and\u2026","rel":"","context":"In &quot;Heroku&quot;","block_context":{"text":"Heroku","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/heroku\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":4038,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/dreamforce-2014-session-create-web-based-ide-for-salesforce\/","url_meta":{"origin":4047,"position":2},"title":"Dreamforce 2014 Session &#8211; create web based IDE for Salesforce","author":"Jitendra","date":"November 7, 2014","format":false,"excerpt":"In Dreamforce 2014, I got chance to present demo of creating online IDE for Salesforce using Tooling API. I used Node.js as a language to communicate with Salesforce API on Heroku Platform. Demo Simple Editor Complete source code (Github) Simple Editor Editor with Ace framework Dont forget to share your\u2026","rel":"","context":"In &quot;Heroku&quot;","block_context":{"text":"Heroku","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/heroku\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":6429,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/heroku\/access-heroku-postgresql-database-locally-using-docker\/","url_meta":{"origin":4047,"position":3},"title":"Access Heroku PostgreSQL Database Locally using Docker","author":"Jitendra","date":"March 11, 2018","format":false,"excerpt":"How to use Heroku Postgres Database from any third party application or local server","rel":"","context":"In &quot;Heroku&quot;","block_context":{"text":"Heroku","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/heroku\/"},"img":{"alt_text":"Heroku Postgres from local PHP Server","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2018\/03\/Heroku-Postgres-from-local-PHP-Server.gif?fit=594%2C674&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2018\/03\/Heroku-Postgres-from-local-PHP-Server.gif?fit=594%2C674&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2018\/03\/Heroku-Postgres-from-local-PHP-Server.gif?fit=594%2C674&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":7147,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-integration-with-nodejs-based-applications-using-canvas\/","url_meta":{"origin":4047,"position":4},"title":"Salesforce Integration with Nodejs based applications using Canvas","author":"Jitendra","date":"July 20, 2020","format":false,"excerpt":"How to use Canvas Signed Request Authentication with web based applications like Nodejs & how to use Canvas LifeCycle Handler Class","rel":"","context":"In &quot;Integration&quot;","block_context":{"text":"Integration","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/integration\/"},"img":{"alt_text":"Salesforce Canvas Integration","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/07\/Salesforce-Canvas-Integration-e1624579092898.png?fit=700%2C567&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/07\/Salesforce-Canvas-Integration-e1624579092898.png?fit=700%2C567&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/07\/Salesforce-Canvas-Integration-e1624579092898.png?fit=700%2C567&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/07\/Salesforce-Canvas-Integration-e1624579092898.png?fit=700%2C567&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":5987,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/use-lightning-components-on-external-websites-lightning-out\/","url_meta":{"origin":4047,"position":5},"title":"Use Lightning Components on external websites &#8211; Lightning Out","author":"Jitendra","date":"April 5, 2017","format":false,"excerpt":"How to use Lightning component in node.js (External websites) by using Lightning Out. It shows how to enable CORS and SSL in Node.js with Video and complete source code.","rel":"","context":"In &quot;Lightning&quot;","block_context":{"text":"Lightning","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/lightning\/"},"img":{"alt_text":"Show Lightning Component in Node.js using Lightning Out","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/04\/Show-Lightning-Component-in-Node.js-using-Lightning-Out.png?fit=1162%2C637&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/04\/Show-Lightning-Component-in-Node.js-using-Lightning-Out.png?fit=1162%2C637&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/04\/Show-Lightning-Component-in-Node.js-using-Lightning-Out.png?fit=1162%2C637&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/04\/Show-Lightning-Component-in-Node.js-using-Lightning-Out.png?fit=1162%2C637&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/04\/Show-Lightning-Component-in-Node.js-using-Lightning-Out.png?fit=1162%2C637&ssl=1&resize=1050%2C600 3x"},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/4047","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=4047"}],"version-history":[{"count":8,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/4047\/revisions"}],"predecessor-version":[{"id":4056,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/4047\/revisions\/4056"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=4047"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=4047"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=4047"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}