13

without having to use php, python or odbc?

ChristopheD
  • 112,638
  • 29
  • 165
  • 179
Benz639
  • 131
  • 1
  • 1
  • 3
  • 5
    Yes, but not the javascript in the browser, which is what I assume you meant? – Jakub Hampl Feb 17 '11 at 13:12
  • Because you didn't specify "node" I have no idea what you're referring to and this is too broad. JavaScript is a langage. There are hundreds of implementations (chrome, chakra) and platforms (server, browser) etc. – Evan Carroll May 30 '18 at 02:07

7 Answers7

14

You can get a JS driver for Postgres from https://github.com/creationix/postgres-js

This one is designed for use with node.js. Don't expect to be able to find something you can run client side in a web browser.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
9

I have used Postgrest (postgrest.com).

"PostgREST is a standalone web server that turns your PostgreSQL database directly into a RESTful API."

Then you can make a query with a url which returns data in json format.

mapsa
  • 464
  • 4
  • 5
4

No, keep in mind that Javascript works client side only when used in a browser while a database can only be connected from server side. Thus you will need to call a serverside script in PHP, Python or another server-side language in order to get to the results.

jsbueno
  • 99,910
  • 10
  • 151
  • 209
Thariama
  • 50,002
  • 13
  • 138
  • 166
  • 10
    JavaScript is not client side only. http://en.wikipedia.org/wiki/Server-side_JavaScript – Quentin Feb 17 '11 at 13:15
  • 1
    There are also browsers with built in SQL databases that JavaScript has access to. https://developer.mozilla.org/en/storage – Quentin Feb 17 '11 at 13:16
  • 1
    the common use case is a browser which i suggest the questioner is asking for, in that case the javascript will only work client side – Thariama Feb 17 '11 at 13:17
3

It is possible. Please see following code. Before using it, you should update Node.js to 7.6.0 or higher. You can use Postgresql by calling only main(yourQuery) function. Found it on Google.

const pg = require('pg')

// create a config to configure both pooling behavior
// and client options
// note: all config is optional and the environment variables
// will be read if the config is not present
var config = {
    user: 'username', // env var: PGUSER
    database: 'databaseName', // env var: PGDATABASE
    password: 'Password', // env var: PGPASSWORD
    host: 'localhost', // Server hosting the postgres database
    port: 35432, // env var: PGPORT
    max: 10, // max number of clients in the pool
    idleTimeoutMillis: 30000 // how long a client is allowed to remain idle before being closed
}


const pool = new pg.Pool(config)

async function query (q) {
    const client = await pool.connect()
    let res
    try {
        await client.query('BEGIN')
        try {
            res = await client.query(q)
            await client.query('COMMIT')
        } catch (err) {
            await client.query('ROLLBACK')
            throw err
        }
    } finally {
        client.release()
    }
    return res
}

async function main (queryStr) {
    try {
        const { rows } = await query(queryStr);
        console.log(JSON.stringify(rows));
    } catch (err) {
        console.log('Database ' + err)
    }
}
main('SELECT * FROM user where user = \'123\'') 
Jasurbek Nabijonov
  • 1,607
  • 3
  • 24
  • 37
3

I never worked with PostgreSQL, but as far as I know Databases require a valid credentials (username and password) to access them. With JavaScript you have no way of hiding the username and password, as the script is sent to the client. So theoretically if you could do that, any client would be able to run queries, and do whatever they want with your database.

Anyways, you cannot access a database from the client side.

David Duponchel
  • 3,959
  • 3
  • 28
  • 36
Dean
  • 7,814
  • 8
  • 30
  • 31
  • 3
    PostgreSQL can authenticate based on client cert btw. Or Kerberos... I think there would be secure ways of doing this from the browser but they would take extra thought. – Chris Travers Jun 05 '13 at 14:19
3

Yes, it is possible if your javascript runs on node.js. Here is connector.

gor
  • 11,498
  • 5
  • 36
  • 42
-2

Nope. Javascript is client-side only. You need some sort of server-side language/interface.

TNC
  • 5,388
  • 1
  • 26
  • 28
  • 11
    There have been server side JavaScript implementations for a decade and a half! http://en.wikipedia.org/wiki/Server-side_JavaScript – Quentin Feb 17 '11 at 13:15
  • 2
    Sorry, was assuming they were referencing browser-based javascript :) – TNC Feb 17 '11 at 13:16