Connect a Railway database(postgreSQL) with node-postgres in Express
Tags: webdev, javascript, database, node
Published: 2022-11-24 20:15:42

There are two options for connecting to Railway's postgres database. Using a connection string or a psql command. This tutorial is going to cover the former(connection string).


## 1. Get a postgres database running
First thing's first, let's get the database up and running.
1.1. Go to [https://railway.app](https://railway.app) and click the "Start a New Project" button.

![Screenshot of the railway homepage](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jmbx3zfklvq3i65etkk1.png)
1.2. Choose the "Provision PostgreSQL" option and wait for it to finish setting up.

![Screenshot of starter options on railway](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u7c5dh3wjirmfo1ilju7.png)

1.3. Select PostgreSQL card.

![Screenshot of starter options on railway](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9qytq9yyz68hqqvucwa5.png)

1.4. Go to the "Connect" tab.

![Screenshot of the connect tab on railway.app](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cfq6tg4h0tfvmy8tlas3.png)

1.5. Copy the "Postgres Connection URL" provided for use in the express app

## 2. Create and setup the express app
2.1. Initialise a javascript project with npm in the project directory
```bash
npm init -y
```
2.2. Install express
```bash
npm install express
```
2.3. Create express app file and setup express

![Screenshot of the directory structure of the express app](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lxclkm4o6l2s1a52vbsi.png)

2.4. Initialise the express app
```js
// In index.js
const express = require("express");
const app = express();

app.get("/", (req, res) => {
  res.send("Hello, World!");
});

const PORT = process.env.PORT || 3001;
app.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
});

```

Okay now to the main idea of this post.

## 3. Connect postgreSQL with express app via `node-postgres`

3.1. Install [node-postgres](https://node-postgres.com/) for interfacing with the database
```sh
npm install pg
```
3.2. Create a separate file for the node-postgres setup.

![Screenshot of the directory structure of the express app with the new file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tkt9i7ajrih6d1u80vpo.png)


3.3. Initialise node-postgres connection passing the connection string from the database as part of the `new Pool` object
```js
// In db.js
const { Pool } = require("pg");

// The secret connection string you copied earlier
const connectionString =
  "postgresql://postgres:dr75i6HaC0sDZDTjtLCt@containers-us-west-37.railway.app:6233/railway";

const pool = new Pool({
  connectionString,
});

module.exports = pool;

```

3.4. Start making calls to your db via express. See example of getting posts from and existing post table
```js
// In index.js
const express = require("express");
const pool = require("./db");
const app = express();

app.get("/", (req, res) => {
  res.send("Hello, World!");
});

app.get("/posts", async (req, res) => {
  const posts = await pool.query("SELECT * FROM posts;");
  res.send({ posts });
});

const PORT = process.env.PORT || 3001;
app.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
});

```

That is it. You can find the code used in this tutorial at [this github repo](https://github.com/Ngoakor12/simple-postgresql-express-example)