Jude Calimbas
Jude Calimbas

Reputation: 2854

Can I use PostgreSQL (pg) in the client-side (express/node.js)

I want to use PostgreSQL in the client side. is that possible? can i have this code?

in my server.js

   var pg = require('pg');

in my client side

     var conString = "postgres://postgres:pass@localhost/mydb";
     var client = new pg.Client(conString);
     client.connect();
     var query = client.query("SELECT * FROM users ");        
     query.on('row', function(row) {
             alert(row.name);
     });

i tried this code but nothing happened. though can i have a code similar to this, where i connect PostgreSQL and use queries on the client scripts.

Upvotes: 8

Views: 9288

Answers (1)

ThiefMaster
ThiefMaster

Reputation: 318598

No, that's (obviously) not possible. You wouldn't want to let a client access your database directly anyway. Besides that, even though you use JS on both the client and server side it's not different from what happens if you use e.g. PHP or Python on the server - the only communication between it is possible via AJAX and regular http requests.

Upvotes: 11

Related Questions