Final answer:
To connect a PostgreSQL database with Node.js, you can use the 'pg-promise' library. Install the library using the command 'npm install pg-promise' and then use the provided code example to establish a connection and execute queries.
Step-by-step explanation:
To connect a PostgreSQL database with Node.js, you can use a popular library called 'pg-promise'. First, you need to install the library by running the command 'npm install pg-promise' in your project directory. Then, you can use the following code as an example:
<code>const pgp = require('pg-promise')();
const db = pgp('postgres://username:passwordlocalhost:5432/database');
// Query example
db.query('SELECT * FROM table')
.then(data => {
console.log(data);
})
.catch(error => {
console.log('Error:', error);
});
Make sure to replace 'username', 'password', and 'database' with your actual details. This code establishes a connection with the PostgreSQL database and allows you to execute queries.