JavaScript and PostgreSQL: A Complete Guide to Integration on Node.js

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Why JavaScript and PostgreSQL Work Together?

\n\n

If you write in JavaScript (especially Node.js) and still use MySQL or, heaven forbid, SQLite in production, you are losing money and nerves. PostgreSQL is not just "another database." It is a Swiss Army knife for data that can do everything: from complex JOINs to JSON documents, full-text search, and geo-queries. And the JavaScript + PostgreSQL combination is the modern standard for web applications, microservices, and analytics.

\n\n

In this guide, we will cover everything a Node.js developer needs to know: from driver installation to transactions, migrations, and optimization. No fluff — only code and practice.

\n\n

Step 1: Installing and Configuring the pg Driver

\n\n

To work with PostgreSQL from JavaScript, the pg package is used — the official client. It installs in seconds:

\n\n
npm install pg
\n\n

Now let's create a connection. There are two approaches: Client (single connection) and Pool (connection pool). For production, always use Pool — it manages connection reuse and does not bring down the database under load.

\n\n
const { Pool } = require('pg');\n\nconst pool = new Pool({\n  user: 'your_user',\n  host: 'localhost',\n  database: 'your_db',\n  password: 'your_password',\n  port: 5432,\n  max: 20,        // max connections in pool\n  idleTimeoutMillis: 30000,\n  connectionTimeoutMillis: 2000,\n});\n\n// Test connection\npool.query('SELECT NOW()')\n  .then(res => console.log('✅ Connected:', res.rows[0].now))\n  .catch(err => console.error('❌ Error:', err));
\n\n

Important: Never hardcode passwords. Use environment variables (process.env.DATABASE_URL) or a secret manager.

\n\n

Step 2: CRUD — The Basics

\n\n

Any application starts with CRUD (Create, Read, Update, Delete). Let's break down each operation using JavaScript with PostgreSQL.

\n\n

Creating a Table (DDL)

\n\n
const createTable = async () => {\n  const query = `\n    CREATE TABLE IF NOT EXISTS users (\n      id SERIAL PRIMARY KEY,\n      name VARCHAR(100) NOT NULL,\n      email VARCHAR(255) UNIQUE NOT NULL,\n      age INTEGER CHECK (age > 0),\n      created_at TIMESTAMP DEFAULT NOW()\n    );\n  `;\n  await pool.query(query);\n  console.log('✅ Table users created');\n};\n\ncreateTable();
\n\n

INSERT — Adding Data

\n\n
const addUser = async (name, email, age) => {\n  // Use parameterized query — protection against SQL injection!\n  const query = `\n    INSERT INTO users (name, email, age)\n    VALUES ($1, $2, $3)\n    RETURNING *;\n  `;\n  const values = [name, email, age];\n  const res = await pool.query(query, values);\n  console.log('✅ User added:', res.rows[0]);\n  return res.rows[0];\n};\n\n// Example call\naddUser('Anna Petrova', 'anna@example.com', 28);
\n\n

SELECT — Reading with Filtering

\n\n
const getUsers = async (minAge = 0) => {\n  const query = 'SELECT * FROM users WHERE age > $1 ORDER BY created_at DESC';\n  const res = await pool.query(query, [minAge]);\n  console.log(`📊 Users found: ${res.rowCount}`);\n  return res.rows;\n};\n\ngetUsers(25).then(users => console.log(users));
\n\n

UPDATE — Updating a Record

\n\n
const updateUserEmail = async (id, newEmail) => {\n  const query = `\n    UPDATE users\n    SET email = $2, updated_at = NOW()\n    WHERE id = $1\n    RETURNING *;\n  `;\n  const res = await pool.query(query, [id, newEmail]);\n  if (res.rowCount === 0) {\n    console.log('⚠️ User not found');\n    return null;\n  }\n  console.log('✅ Email updated:', res.rows[0]);\n  return res.rows[0];\n};
\n\n

DELETE — Deleting

\n\n
const deleteUser = async (id) => {\n  const query = 'DELETE FROM users WHERE id = $1 RETURNING *';\n                

Blogs

Book Recommendations