13

without having to use php, python or odbc?

ChristopheD
117k30 gold badges167 silver badges182 bronze badges
asked Feb 17, 2011 at 13:10
6
  • 5
    Yes, but not the javascript in the browser, which is what I assume you meant? Commented Feb 17, 2011 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. Commented May 30, 2018 at 2:07
  • @EvanCarroll I'm hopeful that in the intervening years you've become less prone towards an urge for closing down good questions due to what you yourself don't know - the error was yours, not ChristopheD's, and Quentin's answer proves it. Commented Jun 10, 2024 at 14:45
  • @RichardT Just re-evaluated the question and casted another close vote, thanks for drawing my attention to it. It's not clear how the author wants to interact with PostgreSQL "connecting postgreSQL directly to Javascript?" needs further clarification, imho. "without having to use php, python or odbc?" doesn't say what you want to do. Commented Jun 10, 2024 at 15:21
  • @EvanCarroll Why do you need to know their motive? The question is reasonable as is: Is there a driver and if so, what is it called? Isn't that enough? Commented Jun 10, 2024 at 15:34

7 Answers 7

16

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.

answered Feb 17, 2011 at 13:12
3
  • That's quite interesting! It's probably very insecure, but thanks a lot for sharing :) Commented Feb 17, 2011 at 13:13
  • 6
    ...for Node.js, not the browser. Commented Feb 17, 2011 at 13:14
  • 1
    Right ... but the question doesn't say "browser" Commented Oct 20, 2016 at 8:17
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.

answered Apr 4, 2017 at 9:51
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
113k11 gold badges159 silver badges238 bronze badges
answered Feb 17, 2011 at 13:12
3
  • 11
    JavaScript is not client side only. en.wikipedia.org/wiki/Server-side_JavaScript Commented Feb 17, 2011 at 13:15
  • 1
    There are also browsers with built in SQL databases that JavaScript has access to. developer.mozilla.org/en/storage Commented Feb 17, 2011 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 Commented Feb 17, 2011 at 13:17
3

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

answered Feb 17, 2011 at 13:15
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
4,0893 gold badges30 silver badges36 bronze badges
answered Feb 17, 2011 at 13:14
2
  • 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. Commented Jun 5, 2013 at 14:19
  • @DavidDuponchel In additino to what Chris pointed out, with Postgres, you can also validate on at least six other ways. See the pg_hba.conf, PGPASSWORD and .pgpass.... Postgres is a FAR more sophisticated system than mysql. Commented Jun 14, 2024 at 17:31
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\'') 
answered Nov 22, 2017 at 18:02
-3

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

answered Feb 17, 2011 at 13:12
2
  • 12
    There have been server side JavaScript implementations for a decade and a half! en.wikipedia.org/wiki/Server-side_JavaScript Commented Feb 17, 2011 at 13:15
  • 2
    Sorry, was assuming they were referencing browser-based javascript :) Commented Feb 17, 2011 at 13:16

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.