The database is a fundamental part of the Evolution API v2, responsible for storing all the application’s critical information. The API supports both PostgreSQL and MySQL, using Prisma as the ORM (Object-Relational Mapping) to facilitate interaction with these databases.

Database Choice

Evolution API v2 offers the flexibility to choose between PostgreSQL and MySQL as the database provider. The choice can be configured through the DATABASE_PROVIDER environment variable, and connections are managed by Prisma.

Installation and Configuration

Using Docker

The easiest and fastest way to set up a database for Evolution API v2 is through Docker. Below are the instructions to configure both PostgreSQL and MySQL using Docker Compose.

PostgreSQL

To set up PostgreSQL via Docker, follow these steps:

  1. Download the docker-compose.yaml file for PostgreSQL available here.
  2. Navigate to the directory where the file was downloaded and run the command:
docker-compose up -d
  1. The PostgreSQL instance will be available at localhost on port 5432.

MySQL

To set up MySQL via Docker, follow these steps:

  1. Download the docker-compose.yaml file for MySQL available here.
  2. Navigate to the directory where the file was downloaded and run the command:
docker-compose up -d
  1. The MySQL instance will be available at localhost on port 3306.

Environment Variables Configuration

After setting up the database, define the following environment variables in your .env file:

# Enable the use of the database
DATABASE_ENABLED=true

# Choose the database provider: postgresql or mysql
DATABASE_PROVIDER=postgresql

# Database connection URI
DATABASE_CONNECTION_URI='postgresql://user:pass@localhost:5432/evolution?schema=public'

# Client name for the database connection
DATABASE_CONNECTION_CLIENT_NAME=evolution_exchange

# Choose the data you want to save in the application database
DATABASE_SAVE_DATA_INSTANCE=true
DATABASE_SAVE_DATA_NEW_MESSAGE=true
DATABASE_SAVE_MESSAGE_UPDATE=true
DATABASE_SAVE_DATA_CONTACTS=true
DATABASE_SAVE_DATA_CHATS=true
DATABASE_SAVE_DATA_LABELS=true
DATABASE_SAVE_DATA_HISTORIC=true

Local Installation

If you prefer to set up the database locally without using Docker, follow the instructions below:

PostgreSQL

  1. Install PostgreSQL on your machine. On Ubuntu-based systems, for example, you can use:
sudo apt-get update
sudo apt-get install postgresql postgresql-contrib
  1. Start the PostgreSQL service:
sudo service postgresql start
  1. Create a database for Evolution API v2:
sudo -u postgres createdb evolution

MySQL

  1. Install MySQL on your machine. On Ubuntu-based systems, you can use:
sudo apt-get update
sudo apt-get install mysql-server
  1. Start the MySQL service:
sudo service mysql start
  1. Create a database for Evolution API v2:
mysql -u root -p -e "CREATE DATABASE evolution;"