PostgreSQL

Using the PostgreSQL client for Deno

In this lesson, we will take a look at PostgreSQL, commonly referred to as Postgres or pgsql/psql/pg. Due to its stability and advanced features, it is the go-to production database system for many.

We will start this lesson by selecting a Postgres client for this course. One of the contributors to Deno, Bartłomiej Iwańczuk, started early with a Postgres library but has handed over the project toDeno Drivers to focus on the development of Deno.

This module is currently the most active and supported one for Postgres, so it will be our client of choice. You can find ithere.

Installation#

For this step, we need two things: a Postgres database and a Deno script. This script will create a connection from our code to our database which will allow us to save and fetch data.

You can either install Postgres locally on your computer by following the guide on the official page, or by using Docker.

Using Docker#

To start the database using Docker, you can run the command below.

You can leave the terminal session open for the rest of this step, and if you get any permission errors, try prefixing the command with sudo.

  • --rm will clean up the container after you kill it. You can read more about it here.

  • -p is short for --port. It links a port on the container with a port on your local system, allowing you to connect to it.

  • -e is short for --env. It is used to pass an env variable to the container.

If you receive any errors regarding port allocation, it probably means that you are already using that port for something else. Try to change the first port to something else e.g. 6543:5432

Writing the connection#

Now it is time for us to move on to the script which will connect our Postgres database to Deno.

As the installation of libraries for Deno is as simple as importing a URL, we can follow the Readme. Start by adding a new file called postgres.ts and add this line of code.

 

This page is a preview of Build and deploy a REST API with Deno

Start a new discussion. All notification go to the author.