CodingNic

Connecting Node.js to PostgreSQL

What Is PostgreSQL?

Connecting Node.js to PostgreSQL 10 min read

What Is PostgreSQL?

Objectives

By the end of this lesson, you should be able to:

  • Explain what a relational database is
  • Explain why a real database replaces the in-memory arrays used throughout Node.js & Express Foundations
  • Describe what PostgreSQL specifically is, in the landscape of relational databases

💡 Why this matters: Every model built in Node.js & Express Foundations stored its data in a plain JavaScript array, gone the moment the process restarted. Before writing a single line of database code, it’s worth being clear on what a real database actually is, and why PostgreSQL is the one this course starts with.

From an Array to a Table

A model like this one, from the previous course, is really just a list of objects living in memory:

javascript
let tasks = [
  { id: 1, title: 'Write lesson', done: false },
  { id: 2, title: 'Review PR', done: true }
];

Restart the Node process, and tasks is gone. A relational database stores the same shape of data, rows with named fields, but on disk, surviving restarts, crashes, and deployments. The array becomes a table, each object becomes a row, each key becomes a column:

text
students
+----+--------+-------+
| id | name   | grade |
+----+--------+-------+
| 1  | Erin   | 9     |
| 2  | Jordan | 10    |
+----+--------+-------+

What “Relational” Means

“Relational” refers to how tables relate to each other, a students table and an enrollments table can reference each other by ID, instead of nesting one inside the other. Module 5 of this course covers exactly how those relationships are modeled. For now, the important part is that a relational database enforces structure: every row in a table has the same columns, and each column has a declared type (TEXT, INTEGER, and so on), unlike a JavaScript array, which happily holds objects with completely different shapes.

Why PostgreSQL

PostgreSQL (often just “Postgres”) is a free, open-source relational database, one of the most widely used in the industry, and the one this course builds on. A few reasons it’s a common default choice:

  • Standards-compliant SQL, the query language covered in Module 2 works largely the same way across relational databases, so the skills transfer.
  • Strong data integrity, PostgreSQL enforces types, constraints, and relationships strictly, catching mistakes a plain array never would.
  • Free and open source, with no license cost, and it runs the same way locally, in Docker, or on any major cloud provider.
  • A large ecosystem, including excellent Node.js support, both the raw pg driver (Modules 1 and 2) and the Prisma ORM (Modules 3 through 6) used later in this course.

What’s Next

The rest of this module gets a real PostgreSQL server running, and connects a Node.js script to it. Nothing here required installing anything yet, that’s the next lesson.

Try It

  1. In your own words, explain the difference between an in-memory array of objects and a table in a relational database.
  2. Look at a model from a previous project (or the task list example above), and sketch what its table would look like, column names and types included.
  3. Name one thing a relational database enforces that a plain JavaScript array does not.

Recap

  • A relational database stores structured, persistent data in tables, rows, and columns, the same basic shape as an array of objects, but on disk and enforced by rules.
  • “Relational” refers to how tables relate to each other through shared IDs, covered in depth in Module 5.
  • PostgreSQL is a free, open-source, standards-compliant relational database with strong Node.js support, the database this course is built around.

Next lesson: installing PostgreSQL and creating your first database.