Adding a database

How to add a database to your application

Every application need to store data in a database at some point. In this guide, you'll learn how to get a production grade database from Qovery in just a few seconds.

Tutorial

Qovery supports most popular SQL and NoSQL databases (You can see the complete list here). In this tutorial we will deploy a PostgreSQL database.

  1. Configuration

    To add a PostgreSQL to our application we simply need to put three mandatory lines in our .qovery.yml file as follow:

    ...
    databases:
    - type: postgresql # database type
    version: "11.5" # database version
    name: my-db # database name (must be unique)
    ...

    Our complete .qovery.yml looks like this

    .qovery.yml
    application:
    name: simple-example-node
    project: getting-started
    cloud_region: aws/us-west-2
    publicly_accessible: true
    databases:
    - type: postgresql
    version: "11.5"
    name: my-db
    routers:
    - name: main
    routes:
    - application_name: simple-example-node
    paths:
    - /

    To start the database you need to commit and push the modification of your .qovery.yml file.

    $ git add .qovery.yml
    $ git commit -m "Add PostgreSQL database to .qovery.yml"
    $ git push -u origin master

    Well done, your database will be ready within a few seconds. To check the readiness status:

    $ qovery status
  2. Connect

    Now, we need to connect our application to our database. The credentials (URI, Username, Password ...) are available through environment variables. They are injected by Qovery when your application runs.

    First, we need to list all the environment variables available for our application:

    # List all environment variables
    $ qovery application env list
    # By adding the arg option -c you can see the value of hidden fields
    Output
    SCOPE | KEY | VALUE
    BUILT_IN | QOVERY_JSON_B64 | <base64>
    BUILT_IN | QOVERY_BRANCH_NAME | master
    BUILT_IN | QOVERY_IS_PRODUCTION | false
    BUILT_IN | QOVERY_APPLICATION_SIMPLE_EXAMPLE_NODE_HOSTNAME | <hidden>
    BUILT_IN | QOVERY_DATABASE_MY_DB_DATABASE | postgres
    BUILT_IN | QOVERY_DATABASE_MY_DB_PASSWORD | <hidden>
    BUILT_IN | QOVERY_DATABASE_MY_DB_USERNAME | <hidden>
    BUILT_IN | QOVERY_DATABASE_MY_DB_PORT | <hidden>
    BUILT_IN | QOVERY_DATABASE_MY_DB_FQDN | <hidden>
    BUILT_IN | QOVERY_DATABASE_MY_DB_HOST | <hidden>
    BUILT_IN | QOVERY_DATABASE_MY_DB_CONNECTION_URI_WITHOUT_CREDENTIALS | <hidden>
    BUILT_IN | QOVERY_DATABASE_MY_DB_CONNECTION_URI | <hidden>
    BUILT_IN | QOVERY_DATABASE_MY_DB_VERSION | 11.5
    BUILT_IN | QOVERY_DATABASE_MY_DB_TYPE | POSTGRESQL
    BUILT_IN | QOVERY_DATABASE_MY_DB_NAME | my-db

    To connect our NodeJS application to our PostgreSQL database, we only have to:

    • Use the NodeJS PostgreSQL client (pg)
    • Use QOVERY_DATABASE_MY_DB_CONNECTION_URI into our code

    Add the pg dependency into package.json

    {
    /* ... */
    "dependencies": {
    /* ... */
    "pg": "^7.17.0"
    }
    }

    Connect our application to PostgreSQL (see documentation)

    const { Pool } = require('pg')
    const pool = new Pool({
    connectionString: process.env.QOVERY_DATABASE_MY_DB_CONNECTION_URI,
    })
    // your can use your connection pool now ...

    Nothing more, well done! You can now be able to use your database.

  3. Deploy

    Commit, and push your code, and you are done. Your application will be deployed.

    $ git add app.js
    $ git commit -m "Update my code to use PostgreSQL"
    $ git push -u origin master

    What the deployment status

    $ qovery status

Next Steps

Congratulations, your application is connected to a PostgreSQL database. Now we will see how to add your custom domain to your service.