Skip to main content
This example deploys a PostgreSQL database and an application in the same environment, and gives the application the connection string of the database. To connect two services to each other, see Linking Services Together.

How services connect

When you create a database, Qovery adds built-in variables to its environment. They hold its connection details, such as its internal host name. To give one of them the name your application reads, declare an alias on the application: environment_variable_aliases for a variable, secret_aliases for a secret. Built-in variable names follow the pattern QOVERY_<TYPE>_Z<ID>_<NAME>:
  • <TYPE> is the database type: POSTGRESQL, MYSQL, MONGODB or REDIS.
  • <ID> is the first eight characters of the ID of the database, in uppercase. In Terraform: upper(split("-", qovery_database.postgres.id)[0]).
  • <NAME> names the value. HOST_INTERNAL, PORT, LOGIN and DEFAULT_DATABASE_NAME are variables, and PASSWORD and DATABASE_URL_INTERNAL are secrets.
Use the _INTERNAL values to connect from a service that runs on the cluster. The complete list of database variables is in Credentials and connectivity. Applications, containers and Helm services have built-in variables too: see Linking Services Together.

Configuration

The database runs in CONTAINER mode, on your cluster. The application reads the connection string from DATABASE_URL, an alias of the database’s DATABASE_URL_INTERNAL secret.
variables.tf
main.tf
At runtime, DATABASE_URL holds a URL of the form postgresql://<login>:<password>@<host>:<port>/<database>. The configuration never handles the password: Qovery resolves the alias when it deploys the application. To deploy, set TF_VAR_organization_id and TF_VAR_cluster_id, then run terraform init and terraform apply, as in the basic application example.

Use a managed database

For production, use a managed database. Managed databases are created from a blueprint of the service catalog, with the qovery_blueprint resource. The Amazon RDS for PostgreSQL blueprint below needs an AWS cluster. Replace qovery_database.postgres, the postgres_variables local and the secret_aliases of the application with:
In qovery_deployment, replace qovery_database.postgres with qovery_blueprint.postgres in depends_on. A blueprint publishes its outputs as variables of the environment, named QOVERY_OUTPUT_TERRAFORM_Z<ID>_<OUTPUT>, where <ID> is the first eight characters of service_id, in uppercase. service_id is the ID of the Terraform service that runs the blueprint. Terraform creates the blueprint and waits for its deployment before it creates the application, so the outputs exist when the application deploys. Reference them through {{...}} interpolation, not with aliases: an alias must be a secret exactly when its target is one, and the blueprint decides which outputs are secrets.

Next steps

Multi-Environment Setup

Deploy the same stack to several environments

Linking Services Together

Connect services to each other with built-in variables

qovery_database reference

Every attribute of the database resource

Database Configuration

Container and managed databases