Query PostgreSQL Data From Node.js

DZone 's Guide to

Query PostgreSQL Data From Node.js

In this article, we will take a look at how to use the API Server to request JSON-formatted PostgreSQL data in Node.js. Read on to get started!

· Big Data Zone ·
Free Resource

The API Server exposes Web services that allow connectivity to your data. Use the OData endpoint of the CData API Server to execute CRUD queries to PostgreSQL data from Node.js.

The CData API Server, when paired with the ADO.NET Provider for PostgreSQL, exposes PostgreSQL data (or data from any of 120+ other ADO.NET Providers) as an OData endpoint, which can be queried from Node.js using simple HTTP requests. This article shows how to use the API Server to request JSON-formatted PostgreSQL data in Node.js.

Set Up the API Server

Follow the steps below to begin producing secure PostgreSQL OData services:


The API Server runs on your own server. On Windows, you can deploy using the stand-alone server or IIS. On a Java servlet container, drop in the API Server WAR file. See the help documentation for more information and how-tos.

The API Server is also easy to deploy on Microsoft Azure, Amazon EC2, and Heroku.

Connect to PostgreSQL

After you deploy the API Server and the ADO.NET Provider for PostgreSQL, provide authentication values and other connection properties needed to connect to PostgreSQL by clicking Settings -> Connections and adding a new connection in the API Server administration console.

To connect to PostgreSQL, set the Server, Port (the default port is 5432), and Database connection properties and set the User and Password you wish to use to authenticate to the server. If the Database property is not specified, the data provider connects to the user's default database.

You can then choose the PostgreSQL entities you want to allow the API Server access to by clicking Settings -> Resources.

Authorize API Server Users

After determining the OData services you want to produce, authorize users by clicking Settings -> Users. The API Server uses authtoken-based authentication and supports the major authentication schemes. Access can also be restricted based on IP address; all IP addresses except the local machine are restricted by default. You can authenticate as well as encrypt connections with SSL.

Consume PostgreSQL OData Feeds From Node.js

OData feeds are easy to work within Node.js. You can use the HTTP client in Node.js to request JSON-formatted data from the API Server's OData endpoint. After making the request, you can construct the body of the response and call the JSON.parse() function to parse it into records.

The code below will make an authenticated request for Orders data. The example URL below applies a simple filter that searches for records with a value of USA in the ShipCountry column. 

var http = require('http');
protocol: "http:",
hostname: "MyServer.com",
port: MyPort,
path: "/api.rsc/Orders?$filter=" + encodeURIComponent("ShipCountry eq 'USA'"),
auth: 'MyUser:MyAuthtoken'
function(res) {
var body = '';
res.on('data', function(chunk) {
body += chunk;
res.on('end', function() {
var jsonData = JSON.parse(body);
}).on('error', function(e) {
console.log("Error: ", e);
big data ,data ingestion ,node.js ,postgresql ,tutorial

Published at DZone with permission of Jerod Johnson , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}