Node.js is one of the most widely used backend JavaScript runtimes, and PostgreSQL is a robust, open-source relational database. Building scalable and effective web applications is possible when you combine these two. We will show you how to install PostgreSQL, create a Node.js project, and link your application to a PostgreSQL database on Ubuntu 20.04.

Requirements

  • An Ubuntu 20.04 server or local environment
  • Node.js and npm installed
  • Access to sudo or root privileges

Step 1: Install PostgreSQL

Start by installing PostgreSQL using Ubuntu’s default package manager.

sudo apt update
sudo apt install postgresql postgresql-contrib

Start and enable the PostgreSQL service:

sudo systemctl start postgresql
sudo systemctl enable postgresql

Check if PostgreSQL is running:

sudo systemctl status postgresql

Step 2: Create a PostgreSQL User and Database

PostgreSQL uses a concept called roles to handle authentication. For our Node.js application, let’s create a new user and database.

1. Switch to the postgres user:

sudo -i -u postgres

2. Open the PostgreSQL shell:

psql

3. Run the following SQL commands:

CREATE USER nodeuser WITH PASSWORD 'nodepassword';
CREATE DATABASE nodeapp;
GRANT ALL PRIVILEGES ON DATABASE nodeapp TO nodeuser;

Exit the shell by typing:

\q

Return to your normal user session:

exit

Step 3: Install Node.js and npm (if not already installed)

If you haven’t installed Node.js yet, you can do so using the NodeSource repository.

curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs

Verify the installation:

node -v
npm -v

Step 4: Set Up a Node.js Project

Let’s integrate the PostgreSQL database with a basic Node.js application.

mkdir node-postgres-demo
cd node-postgres-demo
npm init -y

Installing the pg module is necessary to install the PostgreSQL client for Node.js:

npm install pg

Step 5: Connect to PostgreSQL From Node.js

Create a new file called index.js:

vi index.js

Add the following content:

const { Client } = require('pg');

// PostgreSQL connection config
const client = new Client({
  user: 'nodeuser',
  host: 'localhost',
  database: 'nodeapp',
  password: 'nodepassword',
  port: 5432,
});

// Connect to the database
client.connect()
  .then(() => {
    console.log('Connected to PostgreSQL');
    return client.query('SELECT NOW()');
  })
  .then(res => {
    console.log('Current time from DB:', res.rows[0]);
  })
  .catch(err => {
    console.error('Connection error', err.stack);
  })
  .finally(() => {
    client.end();
  });

Save and exit.

Run the script:

node index.js

You should see output similar to:

Connected to PostgreSQL
Current time from DB: { now: 2025-07-29T10:40:00.000Z }

Conclusion

Integrating PostgreSQL with Node.js on Ubuntu 20.04 is straightforward and gives you the power to build robust database-driven applications. From installation to running queries, this setup serves as the foundation for more advanced features like REST APIs, authentication, and data modeling.

Server Hosting Solutions by RackNerd:

Shared Hosting
cPanel Web Hosting in US, Europe, and Asia datacenters
Logo
Reseller Hosting
Create your new income stream today with a reseller account
Logo
VPS (Virtual Private Server)
Fast and Affordable VPS services - Instantly Deployed
Logo
Dedicated Servers
Bare-metal servers, ideal for the performance-demanding use case.
Logo

Leave a comment

Your email address will not be published. Required fields are marked *