Skip to content

Common packages

Course: Actionable Docker - chapter 6

docker run -d -p 27017:27017 mongo
docker run -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 postgres

The connection string for this postgres would be

postgresql://postgres:mysecretpassword@localhost:5432/postgres
Code to test it out
// Import the pg library
const { Client } = require('pg');
// Define your connection string (replace placeholders with your actual data)
const connectionString = 'postgresql://postgres:mysecretpassword@localhost:5432/postgres';
// Create a new client instance with the connection string
const client = new Client({
connectionString: connectionString
});
// Connect to the database
client.connect(err => {
if (err) {
console.error('connection error', err.stack);
} else {
console.log('connected to the database');
}
});
// Run a simple query (Example: Fetching the current date and time from PostgreSQL)
client.query('SELECT NOW()', (err, res) => {
if (err) {
console.error(err);
} else {
console.log(res.rows[0]);
}
// Close the connection
client.end();
});