How to successfully start with Nest.js and Postgres
Tags: nextjs, postgres, beginners, tutorial
Published: 2022-08-09 18:50:57

In this tutorial will see how to install, configure postgres with Nest.js typeorm.

#Postgres

## 1. Let's install Postgres


Install [postgres on the official website](https://www.postgresql.org/download/). I'll recommend installing it on the official website rather then using homebrew cause it could lead to some trouble later


## 2. Basic of Postgres

- Start Postgres app and start server
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g96a6kzqo5elowgn24vk.png)
 
- Open your terminal and add psql to your path.

export PATH="/Applications/Postgres.app/Contents/Versions/9.5/bin:$PATH"

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/easzqulwxgbu9a18cmv1.png)

### Basic Command

To open psql terminal : `psql postgres`
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tw75e73eg7kgi8fi0eje.png)
  
- List all Database with `\l `
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hwdhe8naw1762dus5fne.png)``
 
- Create a Database with `CREATE DATABASE name;`
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xfrbe8w7mjw9oob9wrcc.png)
 
- Connect to a Database with `\c name`
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uacd5467nk2382y5x6zq.png)

- Exit postgres with `\q`
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iwog92guqfwzf10dxyz0.png)

# Nest

## Setup Nest Project

`npm i -g @nest/cli`
`nest new project-name`

## Create .env file for the config of our Database

Create a .env file containing the PORT, USERNAME, DATABASE NAME, URL ...

`PORT=4242
POSTGRES_PORT=5432
POSTGRES_USER=postgres
POSTGRES_DB=nest
POSTGRES_HOST=localhost`

## Configure App Module

Inside your app module add the .env config file :

`import { ConfigModule } from '@nestjs/config';`
`ConfigModule.forRoot({ envFilePath: ['.env'] })`

## Configure and install TypeOrm

Install Typeorm postgres for Next :

`npm install --save @nestjs/typeorm typeorm postgres`

Then import our configuration inside the app.module :

`import { TypeOrmModule } from '@nestjs/typeorm'`
`TypeOrmModule.forRoot({
      type: 'postgres',
      host: 'process.env.POSTGRES_HOST,
      port: parseInt(process.env.POSTGRES_PORT),
      username: process.env.POSTGRES_USER,
      database: process.env.POSTGRES_DB,
      entities: [],
      synchronize: true,
      logging: true,
      autoLoadEntities: true,
    })`

Little explanation 
`synchronize: true,
 autoLoadEntities: true,`

Allow schema to be automatically with Database.

Little explanation 
`logging: true`

Log all the interaction with the Database

## Create Entity 

Create a user.entity.ts file inside an entities folder.  

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/beuwkhj01gwzu4jse9i2.png)
I won't go into detail about this code cause you can find good documentation on Typeorm website.
 
`import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  CreateDateColumn,
} from 'typeorm';

@Entity()
export class User {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  username: string;

  @Column()
  email: string;

  @Column()
  password: string;

  @Column({
    nullable: true,
    unique: true,
  })
  access_token: string;

  @CreateDateColumn()
  created_at: Date;
}`

Then inside your app.module add `entities: [User]`