Connecting and persisting to a PostgreSQL database
Initially released in 1996, PostgreSQL is an open source object-relational database that is still commonly used. PostgreSQL can be used as both a relational and document database.
In this recipe, we're going to insert into and retrieve data from a PostgreSQL database using the pg
module (https://www.npmjs.com/package/pg).
Getting ready
To get started, we will need a PostgreSQL server to connect to. We will use Docker to provision a containerized PostgreSQL database. Refer to the Technical requirements section of this chapter for more information about using Docker to provision databases.
We will be using the Docker official PostgreSQL image from https://hub.docker.com/_/postgres:
- In a Terminal window, type the following to provision a
postgres
container:$ docker run --publish 5432:5432 --name node-postgres --env POSTGRES_PASSWORD=PASSWORD --detach postgres:12
- Assuming you do not have a copy of the PostgreSQL...