---
description: First, we should connect to the database.
---

# Database connection

Let's create a <mark style="color:blue;">`DatabaseModule`</mark> to connect to the database.

```sh
nest g mo database
```

In its <mark style="color:blue;">`imports`</mark> array, add the following:

```typescript
TypeOrmModule.forRoot({
  type: 'postgres',
  username: 'postgres',
  password: 'pass123',
  host: 'localhost',
  port: 5432,
  database: 'postgres',
  autoLoadEntities: true,
})
```

{% hint style="info" %}
The <mark style="color:blue;">`autoLoadEntities`</mark> option automates the loading of domain's entities.
{% endhint %}

<mark style="color:green;">**Commit**</mark> - Connecting to database

All right, we established a connection to the database. But this solution is not very interesting, don't you think? We are exposing all our credentials here. The same problem appeared again.

> In the **Configuration** section, we'll learn how to fix this issue through the usage of **environment variables** and also have validation/typing for them.
