In the PERN stack, Express never talks to PostgreSQL by itself. A small library called node-postgres, installed as pg, carries every query from your Node.js code to the database and brings the rows back.
Most beginner tutorials hurry past this piece to get to React. We think it is the part to slow down on. The way you pass data into a query decides whether your first app can be broken with one line typed into a URL. This article explains what pg does and walks you through one working route in about 30 minutes, with free tools.
What does pg do in the PERN stack?
PERN stands for PostgreSQL, Express, React and Node.js. We explain the four letters, and how they compare with MERN, in our guide to MERN vs PERN for web development training in Nepal. Here we look only at the line between the E and the P.
React sends a request. An Express route on Node.js receives it. That route needs data, so it hands a SQL query to pg. node-postgres sends the query to PostgreSQL, waits, and returns the result as JavaScript objects. Each row becomes an object whose keys match the column names, so a row from a courses table looks like { id: 1, name: 'Backend' }.
Two parts of pg matter on day one. A Pool keeps a few open connections to the database and lends one to each query. A parameterised query keeps your SQL text and the user's data apart. The rest of the library can wait.
Why a beginner should start with pg, not an ORM
An ORM is a tool like Prisma that lets you work with the database through code instead of writing SQL by hand. Many teams use one, and our backend course teaches Prisma. We still think a beginner should write their first queries with plain pg.
The reason is that pg hides nothing. You write the SQL, you see the SQL, and when it fails, the error comes from PostgreSQL in words you can search. Our course follows the same order. Week 6 starts with core SQL in psql and safe queries, and Prisma comes after that in the same week.
How to set up a PERN stack backend in 30 minutes
You need Node.js 18 or newer, which Express requires, and either Docker or a normal PostgreSQL install. On an 8 GB laptop, run one database at a time and close the browser tabs you do not need.
Step 1: start PostgreSQL
With Docker, run docker run --name pern-db -e POSTGRES_PASSWORD=devpass -p 5432:5432 -d postgres. The official image requires POSTGRES_PASSWORD, and it creates a user called postgres. The -p 5432:5432 part opens PostgreSQL's usual port to your own machine, so Node.js can reach it. The official postgres image page on Docker Hub lists the other settings.
If Docker Desktop is too heavy for your laptop, install PostgreSQL from postgresql.org instead. The steps below stay the same.
Step 2: make one table
Open the database shell with docker exec -it pern-db psql -U postgres. psql is the terminal front end that comes with PostgreSQL. Type CREATE TABLE courses (id serial PRIMARY KEY, name text NOT NULL); and then INSERT INTO courses (name) VALUES ('Backend'), ('QA');. Check it with \dt, which lists your tables, then leave with \q.
Step 3: install Express and pg
Make a new folder, open a terminal in it, and run npm init -y and then npm install express pg. That is all the backend needs for now.
Step 4: write the route
Create index.js. At the top, load the two libraries with const express = require('express') and const { Pool } = require('pg'). Then make the app with const app = express() and a pool with const pool = new Pool({ connectionString: 'postgres://postgres:devpass@localhost:5432/postgres' }).
Now add one route. Inside app.get('/courses/:id', async (req, res) => { ... }), run const result = await pool.query('SELECT * FROM courses WHERE id = $1', [req.params.id]) and then res.json(result.rows). End the file with app.listen(3000).
The $1 is a placeholder. The array after the query holds its value. node-postgres sends the two to PostgreSQL separately, and the database puts the value in place itself.
Step 5: call it
Run node index.js. In a second terminal, run curl localhost:3000/courses/1. You should see the Backend row as JSON. Change the 1 to a 2 and you get the QA row.
Break it on purpose
Now change the query to the way many beginners write it the first time: 'SELECT * FROM courses WHERE id = ' + req.params.id. Restart the server and run curl "localhost:3000/courses/1%20OR%201=1". The %20 is a space in a URL.
You get every row in the table. The text 1 OR 1=1 became part of your SQL, and 1=1 is true for every row. This is SQL injection. The OWASP cheat sheet on preventing SQL injection describes it as untrusted data becoming part of a query's SQL syntax. On a table of users or payments, the same mistake leaks real data.
Put the $1 version back and try the same URL. PostgreSQL refuses it, because 1 OR 1=1 is not a whole number, so the request fails instead of returning the table. In a real app you catch that error and send back a clear 400 response. The node-postgres guide to queries warns against joining user input into query text, because it often leads to SQL injection.
Why use a Pool and not a single Client?
node-postgres also has a Client, which is one connection. The library's own docs say a web app should use a pool. Opening a new connection takes a handshake of 20 to 30 milliseconds, and one client runs only one query at a time. A pool keeps connections open and shares them. pool.query also gives the connection back for you, so you cannot forget to return it.
A learner testing alone with curl will not see the difference. You will see it the first time many requests arrive at once, so it is better to start with the habit.
What to try tonight
Do the five steps above. Then add a second route that saves a course with INSERT INTO courses (name) VALUES ($1) RETURNING *, and send the name in a POST request with curl. Add app.use(express.json()) near the top so Express can read the JSON you send. If both routes work and the injection test fails safely, you have built the backend half of a PERN app by hand.
Our backend engineering course in Nepal covers this ground in depth. Week 6 is SQL, PostgreSQL, SQL injection and parameterised queries, Prisma, and about two hours of MongoDB. Week 7 adds indexes, transactions and connection pools. The week-by-week backend engineering syllabus is public, so you can read every topic before you decide.
Common questions
How to learn the PERN stack from zero?
Learn JavaScript first, then build a small Express API, then connect it to PostgreSQL with pg, and add React last. That order puts the hardest part, the data, early. Our backend course starts the same way, with JavaScript and TypeScript in Weeks 2 and 3.
Is PostgreSQL hard for beginners?
PostgreSQL for beginners is mostly SQL: creating tables, adding rows, and writing a SELECT with a JOIN. You can start those basics in one evening with the official PostgreSQL tutorial. Harder topics like indexes and transactions can wait until your first full app works.
Should a PERN stack developer use pg or Prisma?
Learn both, in that order. Many teams use an ORM like Prisma at work, and it still sends SQL to PostgreSQL underneath. A developer who can write that SQL with pg can read what the ORM is doing when a query is slow or wrong.
Do I need Docker for a PERN stack tutorial?
No. Docker is the quickest way to run PostgreSQL and remove it later, but a normal install from postgresql.org works the same with pg. Our course installs Docker Desktop as Week 1 homework, because Week 7 runs the whole app with Docker Compose.
