Node.js
Relyt supports the PostgreSQL connection protocol, which means any tools or software that can connect to PostgreSQL can also be used for Relyt connections.
This guide will walk you through the process of connecting to Relyt using Node.js.
1. Prepare the environment
Install node module pg:
npm install pg
2. Create a Node.js program
-
Create a .js file:
vim relyt_demo.js
-
Write the program code in the file:
const { Client } = require('pg');
const client = new Client({
host: '<endpoint_domain>',
port: <endpoint_port>,
user: '<dw_user>',
password: '<dw_user_password>',
database: '<database>'
});
client.connect();
client.query('DROP TABLE IF EXISTS items;', (err, res) => {
if (err) throw err;
});
client.query('CREATE TABLE IF NOT EXISTS items (id INTEGER, name VARCHAR(50));', (err, res) => {
if (err) throw err;
});
client.query("INSERT INTO items (id, name) VALUES (1, 'item1'), (2, 'item2');", (err, res) => {
if (err) throw err;
});
client.query('SELECT * FROM items;', (err, res) => {
if (err) throw err;
for (let row of res.rows) {
console.log(row);
}
client.end();
});The following table describes the properties contained in the
new Client()
constructor. Specify the properties based on your actual conditions.Property Description host The public endpoint for connecting to the Relyt DW service unit. port The port for connecting to the Relyt DW service unit. The default port is 5432. user The name of the DW user that connects to the Relyt DW service unit. password The password of the DW user. database The name of the Relyt database to connect.
The SQL commands provided in the example above perform the following operations in sequence:
-
Drop a table
-
Create a table
-
Insert data to a table
-
Query data
They are for reference only. You can replace them with any SQL commands you want to run.
-
3. Run the program
Run the program:
node relyt_demo.js