Connecting to the Postgres server using Nest Js project
Notations
This project is divided into 4 parts
- My Computer setup
- Prerequisites
- Installation
- Project setup
- Explanation
My Computer setup
- OS: Linux x64 Arch
- Kernal- 1.10.42-1
- Distrubution- Manajro KDE
- Code editor: VS CODE-Insiders
- Ram - 16 GB
- AMD ryzen 7 pro
Prerequisites
- This tutorial assumes that you have a basic knowledge of Node js, Postgres, and Docker
- Node and npm installed
- Docker Installed
Installation
PART-1 ( Docker )
Pull postgres Image
docker run --rm --name pg-docker -e POSTGRES_PASSWORD=poc-estm -d -p 5432:5432 -v $HOME/docker/volumes/postgres:/var/lib/postgresql/data postgres
--rm
is used to remove the constaine if it exists
--name
is used to specify the name of the container
-e
environment variables
-d
in detached mode
-p
port number to run on
-v
volume to store the data internal to the container on
postgres
the image to pull
The below command will interactively log in to the TTY terminal of the container
docker exec -it pg-docker bash
once inside the psql prompt login to the DB and create a DB of ur choice (will be used in the code )
psql -h localhost -U postgres -d postgres
docker docs referances
PART-2 ( Nest Installation )
Install nest Globally
npm i -g @nestjs/cli
create a new project in the desigred location using the following command
nest new <project-name>
Project Setup
Install the required Dependencies
run the following command to install the dependicies for typeorm and postgres
npm install --save @nestjs/typeorm typeorm mysql2
now run the following command in the new project folder created
nest generate module db
this command will generate
- create a folder named
db
- a module named
db.module.ts
in the abovedb
folder we will need 2 other files basically interfaces and classesdb.interfaces.ts
thedb.errors.ts
lets now populate ourdb.module.ts
file
The most important part of this article
I am using convict
to set up the environment variables convict
If you look at lines 26
to 36
getConnectionOptions()
this function is responsible for creating the JSON that will act as the connector in Nest
- The
type
is the type of the database that you want to connect to it can be (PostgreSQL, Oracle, Microsoft SQL Server, SQLite, and even NoSQL databases like MongoDB) -
host
the thedbhost
-
post
is the port you want to use to connect to the DB in our case it is 5432` - username and password for the DB
- the
database
databaseName -
keepConnectionAlive
is necessary to keep a connection open with the DB - the remaining part of the code we will revisit in the next part of the article
!!! you can also use a db URL to connect to the database in that case just replace the entire JSON from line
23
to29
withurl:<db-url>
app.module.ts
update your app.module.ts
to look like above
The forRoot()
is important here it is responsible for returning a DynamicModule which will create the connection for us with the Postgres DB
Please ignore lines 3 and 4 I will come to it in the next part of the article :)
Now we are ready to test ;)
run the following command in the terminal-
npm run start:dev
If all goes well you will get no errors in case of any please revisit the steps to check what is different.
See you in the next part 😀
Top comments (0)