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.
For production, you can run the database with your cloud provider’s managed service: set mode = "MANAGED" and instance_type, such as db.t3.medium on AWS, and remove cpu and memory. The multi-environment example does this. mode cannot change after creation.

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