xxxxxxxxxx
const { Pool } = require('pg');
// Create a new pool instance with the database connection details
const pool = new Pool({
user: 'your_username',
host: 'your_host',
database: 'your_database',
password: 'your_password',
port: your_port,
});
// Example query
const query = 'SELECT * FROM your_table';
// Use pool.query() to execute the query
pool.query(query, (err, res) => {
if (err) {
console.error('Error executing query', err);
} else {
// Process the result
console.log(res.rows);
}
// Close the pool connection
pool.end();
});
xxxxxxxxxx
// callback
client.query('SELECT NOW() as now', (err, res) => {
if (err) {
console.log(err.stack)
} else {
console.log(res.rows[0])
}
})
// promise
client
.query('SELECT NOW() as now')
.then(res => console.log(res.rows[0]))
.catch(e => console.error(e.stack))
xxxxxxxxxx
const { Client } = require('pg')const client = new Client();(async () => { await client.connect() const res = await client.query('SELECT $1::text as message', ['Hello world!']) console.log(res.rows[0].message) // Hello world! await client.end()})()