Connect to a PostgreSQL DB - (PostgreSQL Database)
PostgreSQL is a powerful, open-source relational database management system that supports SQL and has many advanced features. In this tutorial, we'll show you how to connect to a PostgreSQL database using Node.js.
Syntax
const { Pool, Client } = require('pg')
const pool = new Pool({
user: 'dbuser',
host: 'database.server.com',
database: 'mydb',
password: 'secretpassword',
port: 5432,
})
pool.query('SELECT NOW()', (err, res) => {
console.log(err, res)
pool.end()
})
const client = new Client({
user: 'dbuser',
host: 'database.server.com',
database: 'mydb',
password: 'secretpassword',
port: 5432,
})
client.connect()
client.query('SELECT NOW()', (err, res) => {
console.log(err, res)
client.end()
})
Pool
: Creates a connection pool to the database.Client
: Creates a single-use client to query the database.user
: The username used to authenticate with the database.database
: The name of the database to connect to.host
: The server hosting your database. Defaults tolocalhost
.password
: The password used to authenticate with the database.port
: The port used to connect to the database. Defaults to5432
.
Example
Let's take a look at an example of connecting to a PostgreSQL database using Node.js.
const { Pool } = require('pg')
const pool = new Pool({
user: 'dbuser',
host: 'localhost',
database: 'mydb',
password: 'mypassword',
port: 5432,
})
pool.query('SELECT * FROM users', (err, res) => {
console.log(err, res.rows)
pool.end()
})
In this example, we establish a connection to a PostgreSQL database located on the local machine. We then issue a SQL query to select all rows from the users
table and log the resulting rows to the console.
Explanation
To connect to a PostgreSQL database using Node.js, we need to first install the pg
package by running the following command:
npm install pg
Once we have the package installed, we can create a connection pool or single-use client to interact with the database. In the example above, we used a connection pool (Pool
) to establish a connection to the database and issue a query to select all rows from the users
table.
We then logged the resulting rows to the console and closed the connection pool using the pool.end()
method.
Use
Connecting to a PostgreSQL database using Node.js allows you to easily interact with and manipulate data stored in the database. You can use it to insert, update, delete, select and manipulate data in a PostgreSQL database in real-time.
Important Points
- Be careful not to store database credentials in your code or commit them to version control. Use environment variables or a configuration file to store credentials safely.
- Always close database connections when you're done with them to avoid leaving unused connections open and potentially causing performance issues and memory leaks.
Summary
In this tutorial, we showed you how to connect to a PostgreSQL database using Node.js. We discussed the syntax, example, output, explanation, use, and important points of connecting to a PostgreSQL database using Node.js. With this knowledge, you can now connect to a PostgreSQL database using Node.js and begin interacting with your data in real-time.