# Database Configuration

The database is essential for storing critical data such as users, teams, projects, APIs, and other information required for the proper functioning of the application. By default, the application does not come with any pre-configured database settings.

## [MySQL](https://hub.docker.com/_/mysql)

The minimum recommended version for MySQL is `8.0.17`, and the `utf8mb4` character set is required to ensure full support for Unicode characters. You can provide the connection details to MySQL by configuring the appropriate environment variables as shown below:

```shell
-e MYSQL_DATABASE=apidog \
-e MYSQL_PORT=3306 \
-e MYSQL_USER_NAME='<username>' \
-e MYSQL_PASSWORD='<password>' \
-e MYSQL_HOST='<host>' \
```

Apidog does not automatically create the database for you. You will need to manually create the database using the following SQL statement:

```sql
CREATE DATABASE IF NOT EXISTS apidog CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
```

***Note:*** There is no need to manually create any tables. Apidog will automatically create the necessary tables on startup. However, keep in mind that Apidog will be connecting to the database from within your Docker container.

To ensure a successful connection, make sure that the `MYSQL_HOST` is configured correctly:
    - Option (a): Use a fully qualified hostname.
    - Option (b): Ensure there is a proper entry in your container's `/etc/hosts` file.


By providing the MySQL connection details as mentioned above, **Apidog** will be able to connect to and locate the application database.

## [Redis](https://hub.docker.com/_/redis)

The minimum recommended versions for Redis `6.0.5`. You can provide the Redis connection details by configuring the following environment variables:

```shell
-e REDIS_HOST='<host>' \
-e REDIS_PORT=6379 \
-e REDIS_PASSWORD='<password>' \
-e REDIS_DB=0 \
```

Apidog will be connnecting to Redis **from within your Docker container**. To ensure scuccessful connection, make sure that `REDIS_HOST` is configured correctly:
        - Option (a): Use the fully qualified hostname.
        - Option (b): Ensure tere is a proper entry for Redis in your container's `/etc/hosts` file.


## [PostgreSQL](https://hub.docker.com/_/postgres)

The minimum recommended version for PostgreSQL is `15.0`. You can provide the PostgreSQL connection details by configuring the following environment variables:

```shell
-e DB_DIALECT=postgresql \
-e PG_HOST='<host>' \
-e PG_PORT=5432 \
-e PG_USER='<username>' \
-e PG_PASSWORD='<password>' \
-e PG_DATABASE=apidog \
```

Apidog does not automatically create the database for you. You will need to manually create the database and the default schema `(app)`. Below are the ways to do this:

### Solution 1: Using `psql` (The Command-Line Tool)

This is the most common way. You create the database, then reconnect to it. `psql` has a special meta-command `\c` or `\connect` for this.

1.  Connect to the default `postgres` database:

    ```bash
    psql -U postgres
    ```

2.  Run your commands, using `\c` to switch databases:

    ```sql
    CREATE DATABASE apidog
        WITH
        OWNER = postgres
        TEMPLATE = template0
        ENCODING = 'UTF8'
        LC_COLLATE = 'C'
        LC_CTYPE = 'C';

    -- Now, disconnect from 'postgres' and reconnect to 'apidog'
    -- This is a psql meta-command, NOT a SQL command.
    \c apidog

    -- You are now connected to the 'apidog' database. This command will succeed.
    CREATE SCHEMA IF NOT EXISTS app;
    ```

### Solution 2: Using a GUI Tool (DBeaver, pgAdmin, DataGrip)

The workflow is more manual but follows the same principle.

1.  Connect your GUI tool to the PostgreSQL server (this usually establishes a default connection to the `postgres` database).
2.  Open a query window and run only the `CREATE DATABASE` command.
    ```sql
    CREATE DATABASE apidog
        WITH
        OWNER = postgres
        TEMPLATE = template0
        ENCODING = 'UTF8'
        LC_COLLATE = 'C'
        LC_CTYPE = 'C';
    ```
3.  **Refresh** your database list in the GUI's sidebar. You will see `apidog` appear.
4.  **Open a new connection/query panel specifically for the `apidog` database.** Most GUIs let you right-click the new database and select "New Query Console" or "SQL Editor".
5.  In that **new panel**, run your `CREATE SCHEMA` command.
    ```sql
    CREATE SCHEMA IF NOT EXISTS app;
    ```

No need to add any tables. Apidog will create those on startup.

