# Database Setup

### **Prerequisites**

1. **Install Docker Desktop**: Download and install [Docker Desktop](https://www.docker.com/products/docker-desktop/).
2. **Verify Docker Installation**: Ensure Docker is running and verify with `docker --version` in your terminal.

***

### Docker Configuration

1. **Use a Docker Compose File**: Below is a `docker-compose.yml` for setting up a PostgreSQL database.
2. **Specify Environment Variables**: Update `POSTGRES_DB`, `POSTGRES_USER`, and `POSTGRES_PASSWORD` with your preferred values.

{% code title="docker-compose.yml" %}

```yaml
version: '3.8'

services:
  db:
    image: postgres
    deploy:
      restart_policy:
        condition: on-failure
        delay: 10s
        max_attempts: 3
    environment:
      POSTGRES_DB: #your_prefered_db_name
      POSTGRES_USER: #your_prefered_username
      POSTGRES_PASSWORD: #your_prefered_password
    ports:
      - 5432:5432
    volumes:
      - db:/var/lib/postgresql/data

volumes:
  db:
    driver: local
```

{% endcode %}

3. **Run Docker Container**:

* To create and run the PostgreSQL container, run:

  ```bash
  docker-compose up -d
  ```
* To stop and remove the container:

  ```bash
  docker-compose down
  ```
* To stop, remove, and clear volumes:

  ```bash
  docker-compose down --volumes
  ```

***

### Database Migrations with TypeORM

After setting up your Docker container, you can manage your database schema using TypeORM’s migration tools.

**Main Commands**

TypeORM supports several commands for managing database migrations, which are implemented as part of the project’s setup.

* **Generate a New Migration**: Run this command when you modify any entities to keep the database in sync.

  ```bash
  yarn run migration:generate src/db/migrations/MigrationName
  ```

  * **Example**: `yarn run migration:generate src/db/migrations/AddNewColumn`
  * Make sure to specify the correct path and name your migration descriptively.
* **Run Migrations**: Apply pending migrations to the database.

  ```bash
  yarn run migration:run
  ```
* **Seed the Database**: Populate the database with initial or sample data.

  ```bash
  yarn run seed
  ```

  * **Note**: Seeders are executed in alphabetical order, so name them accordingly if they are dependent on each other.

#### Seeds

Seeds are predefined data sets used to populate the database with initial or sample data. For this application, seeds include essential predefined data such as user roles, team roles, and super admin credentials, which are crucial for initializing the system with baseline configurations.

\
Setup

1. Run database migrations to ensure the database structure is up to date:

   ```bash
   yarn run migration:run
   ```
2. Populate the database with initial data by running seeds:

   ```bash
   yarn seed
   ```

This will set up predefined configurations like user roles, team roles, and super admin credentials, preparing the database for use in development or production environments.

***

### Verification

To verify everything is set up correctly:

* **Confirm Docker is Running**: Check if the PostgreSQL service is running in Docker Desktop.
* **Access the Database**: Connect to the PostgreSQL instance via a database client like **pgAdmin** or **DataGrip** using `localhost:5432`.

With these steps, you now have a fully Dockerized PostgreSQL setup along with commands for managing migrations and seeding data in a project.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://intercode.gitbook.io/intercode-saas-kit/getting-started/database-setup.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
