-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpg.js
37 lines (30 loc) · 930 Bytes
/
pg.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const { Client } = require('pg')
// Import the dotenv package
require('dotenv').config()
// Database connection configuration
const dbConfig = {
user: process.env.PG_USER,
password: process.env.PG_PW,
host: process.env.PG_HOST,
port: process.env.PG_PORT,
database: process.env.PG_DB,
}
// Create a new client instance
const client = new Client(dbConfig)
// Connect to the database
async function connectAndQuery() {
try {
await client.connect()
console.log('Connected to PostgreSQL database')
// Execute SQL queries here
const result = await client.query('SELECT data FROM test WHERE id = 2')
console.log('Query result:', result.rows)
// Close the connection when done
await client.end()
console.log('Connection to PostgreSQL closed')
} catch (err) {
console.error('Error connecting or querying PostgreSQL database', err)
}
}
// Call the async function
connectAndQuery()