without having to use php, python or odbc?
-
5Yes, but not the javascript in the browser, which is what I assume you meant?Jakub Hampl– Jakub Hampl2011年02月17日 13:12:26 +00:00Commented 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.Evan Carroll– Evan Carroll2018年05月30日 02:07:07 +00:00Commented 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.Richard T– Richard T2024年06月10日 14:45:24 +00:00Commented 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.Evan Carroll– Evan Carroll2024年06月10日 15:21:53 +00:00Commented 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?Richard T– Richard T2024年06月10日 15:34:00 +00:00Commented Jun 10, 2024 at 15:34
7 Answers 7
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.
-
That's quite interesting! It's probably very insecure, but thanks a lot for sharing :)Siewers– Siewers2011年02月17日 13:13:40 +00:00Commented Feb 17, 2011 at 13:13
-
6...for Node.js, not the browser.Matt Ball– Matt Ball2011年02月17日 13:14:21 +00:00Commented Feb 17, 2011 at 13:14
-
1Right ... but the question doesn't say "browser"GreenAsJade– GreenAsJade2016年10月20日 08:17:37 +00:00Commented Oct 20, 2016 at 8:17
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.
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.
-
11JavaScript is not client side only. en.wikipedia.org/wiki/Server-side_JavaScriptQuentin– Quentin2011年02月17日 13:15:25 +00:00Commented Feb 17, 2011 at 13:15
-
1There are also browsers with built in SQL databases that JavaScript has access to. developer.mozilla.org/en/storageQuentin– Quentin2011年02月17日 13:16:51 +00:00Commented Feb 17, 2011 at 13:16
-
1the common use case is a browser which i suggest the questioner is asking for, in that case the javascript will only work client sideThariama– Thariama2011年02月17日 13:17:48 +00:00Commented Feb 17, 2011 at 13:17
Yes, it is possible if your javascript runs on node.js. Here is connector.
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.
-
3PostgreSQL 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– Chris Travers2013年06月05日 14:19:29 +00:00Commented 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.Richard T– Richard T2024年06月14日 17:31:39 +00:00Commented Jun 14, 2024 at 17:31
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\'')
Nope. Javascript is client-side only. You need some sort of server-side language/interface.
-
12There have been server side JavaScript implementations for a decade and a half! en.wikipedia.org/wiki/Server-side_JavaScriptQuentin– Quentin2011年02月17日 13:15:00 +00:00Commented Feb 17, 2011 at 13:15
-
2Sorry, was assuming they were referencing browser-based javascript :)TNC– TNC2011年02月17日 13:16:21 +00:00Commented Feb 17, 2011 at 13:16