| title | Database |
|---|---|
| icon | database |
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.
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.
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.
To set up PostgreSQL via Docker, follow these steps:
- Download the
docker-compose.yamlfile for PostgreSQL available here. - Navigate to the directory where the file was downloaded and run the command:
docker-compose up -d- The PostgreSQL instance will be available at
localhoston port5432.
To set up MySQL via Docker, follow these steps:
- Download the
docker-compose.yamlfile for MySQL available here. - Navigate to the directory where the file was downloaded and run the command:
docker-compose up -d- The MySQL instance will be available at
localhoston port3306.
If you don't want to have separate containers, you can create a single file with the database and the Evolution API. Follow the steps below:
- Download the
docker-compose.yamlfile for PostgreSQL available here. - Open the file and add your Evolution API configuration specifying the
networks. For example:
services:
# Your Evolution API configuration:
evolution-api:
...
networks:
- evolution-net
...
# PostgreSQL configuration you downloaded:
postgres:
...
pgadmin:
...
volumes:
postgres_data:
pgadmin_data:
evolution_instances:
networks:
evolution-net:
name: evolution-net
driver: bridge- Save the file and run the command:
docker-compose up -d- Download the
docker-compose.yamlfile for MySQL available here. - Open the file and add your Evolution API configuration specifying
networks. For example:
services:
# Sua configuração do Evolution API:
evolution-api:
...
networks:
- evolution-net
...
# Configuração do MySQL que você baixou:
mysql:
...
volumes:
mysql_data:
evolution_instances:
networks:
evolution-net:
name: evolution-net
driver: bridge- Save the file and run the command:
docker-compose up -dAfter 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=trueIf you use a single docker-compose.yaml, you should replace localhost with the name of your database service. For example:
# Database connection URI
DATABASE_CONNECTION_URI='postgresql://user:password@postgres:5432/evolution?schema=public'# Database connection URI
DATABASE_CONNECTION_URI='postgresql://user:password@mysql:5432/evolution?schema=public'If you prefer to set up the database locally without using Docker, follow the instructions below:
- 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- Start the PostgreSQL service:
sudo service postgresql start- Create a database for Evolution API v2:
sudo -u postgres createdb evolution- Install MySQL on your machine. On Ubuntu-based systems, you can use:
sudo apt-get update
sudo apt-get install mysql-server- Start the MySQL service:
sudo service mysql start- Create a database for Evolution API v2:
mysql -u root -p -e "CREATE DATABASE evolution;"