Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Use Node.js net.Socket To Communicate with Postgresql Database Like One Would Using Terminal

In the postgresql terminal I can type a command and get a response. For example:

# create database new database;

CREATE DATABASE

Above I enter in the terminal the command “create database newdatabase;” The database program responds “CREATE DATABASE”

I am trying to do the same the thing but using a node net.Socket. In other words, I want to issue commands to the postgresql server I have running on localhost:5432 and get a response back via a socket.

My program successfully sets up a connection with the postgresql server and successfully flushes the data to the kernel, but I never get a response (the data listener never gets fired). The newdatabase does not get created either.

I also took a quick look at what happens on wireshark. It looks like the socket gets setup. I see my data get sent in cleartext. The postgresql server then sends an ACK FIN ACK. I ACK FIN ACK and then the postgresql server ACKs one last time. So I know that the postgresql server does not send any data back.

My question is, why does the postgresql server ignore the command I send it and why does the postgresql server not send me any data back, even if that data is just an error.

const net = require('net');
const BlueBird = require('bluebird');

BlueBird.coroutine(function* () {

var host = "127.0.0.1";
var port = "5432";
var idle_timeout = 10000;

var MySocket = new net.Socket();
MySocket.setTimeout(idle_timeout);

var data = yield new Promise(

    function resolver(resolve, reject) {

        MySocket.on('connect', function () {
            var flushed = MySocket.write("create database newdatabase;", "utf8");
            console.log("Data flushed to kernel: " + flushed);
        });

        MySocket.on('data', function (data) {
            console.log(data);
            resolve(data);
        });

        MySocket.on('error', function (error) {
            reject(error);
        });

        MySocket.connect(port, host);

    }

    );

    return data;

})()
.then(function (data) {

    console.log(data);

    return data;

})
.catch(function (error) {

    console.error(error);

})
like image 349
TheGreg Avatar asked Feb 25 '16 11:02

TheGreg


Video Answer


1 Answers

psql is a (REPL) command line tool which takes a command entered on one or several lines, and parses it, and sends it to the database.

PostgreSQL is not talking the same textbased protocol over the socket on port 5432. You can read more about PostgreSQL protocol in their documentation.

Use the pg module to connect to Postgres with node, and do the queries there:

var pg = require('pg');
var conString = "postgres://username:password@localhost/database";

pg.connect(conString, function(err, client, done) {
  if(err) {
    return console.error('error fetching client from pool', err);
  }
  client.query('create database newdatabase', function(err, result) {
    console.log('CREATE DATABASE');
  });
}    
like image 163
bolav Avatar answered Nov 15 '22 00:11

bolav