Node.js Application featured image

Connecting PostgreSQL with Node.js Applications: A Tutorial

When it comes to database management, PostgreSQL is one of the most popular open-source programs used all across the board. The CloudSigma PaaS allows you to install either a standalone or clustered PostgreSQL database for your application. Furthermore, CloudSigma PaaS makes connection configuration and scaling an absolute breeze.

So how can you go about connecting your Node.js application to a PostgreSQL server? In this guide, we will give you a step-by-step tutorial on how to make these connections with ease. 

Let’s begin!

Connect a Node.js Application to PostgreSQL on CloudSigma PaaS

Step 1. First, log into your CloudSigma account and head over to the PaaS dashboard. Create a New Environment with the PostgreSQL database. Then, add a Node.js compute node and set the scaling limits for each server as required:

Node.js Application 1

Step 2. Click on the Web SSH icon to access your Node.js server through SSH:

Node.js Application 2

Step 3. Once you connect via the SSH client, install node-postgres. This is a group of Node.js modules that you need to interface with the PostgreSQL database. You can install the modules using this command:

Wait for node-postgres to finish installation:

Node.js Application 3

Step 4. Next, we need to make a Node.js script in order to verify the connection between the database and your application.

With node-postgres, you can have client as well as pool connections. Client connections are static connections while pools have a dynamic list of client objects which can reconnect automatically. In our example, we will be creating a pool connection so that we can accommodate multiple requests simultaneously.

To start off, make a file with a .js extension in any text editor. Paste the following code into the file to create a connection with the database and perform a query:

You will need to replace the placeholders in this code with your own connection data. Here are the parameters:

  • {user}

The username you use to log into the database.

  • {password}

The password for the above-mentioned user.

  • {host}

The link to your PostgreSQL container.

  • {database}

The database you want to access. In this case, the default database is PostgreSQL.

  • {port}

The port number the database server listens to. The default port for PostgreSQL is 5432.

You will receive this connection information in an email after you install PostgreSQL. This is what your email should look like:

Node.js Application 4

The script will look something like this:

Once you have entered the script, press Ctrl+X to save. Once the system prompts you to save the changes, press Ctrl+Y to save and proceed.

In the body of the script, the node-postgres can use the environment variables for the connection instead of your credentials. They are as follows:

  • GUSER={user}
  • PGHOST={host}
  • PGPASSWORD={password}
  • PGDATABASE={database}
  • PGPORT={port}

Step 5. You can use this script with a particular command to check the connection to the database from your application server. You can also request the current local time value from the database server node. Use this command to do so:

If the connection is successful, you will see a PostgreSQL server node time like this:

This means that the database container is accessible for your Node.js application!

Open-source solutions like PostgreSQL provide a lot of utility when it comes to app development and database management. Such solutions are also useful for replication and auto-clustering

CloudSigma PaaS looks to make the process even simpler for Node.js-based applications. If you would like to learn more about what the CloudSigma PaaS can help you achieve, head over here. Give CloudSigma PaaS a try with our 7-day free trial or contact us if you want us to provide you with more details and a platform demo.

Try Free for 7 days

About Zhenya Mocheva

Zhenya is a Digital Marketing Expert at CloudSigma, focusing on brand strategy, social media marketing and digital marketing campaigns. She is passionate about the continuous innovation within the digital environment and the endless growth opportunities that inbound marketing brings.