Adding a database in garden

Adding a database to a garden project

You will need to add 2 new modules to your garden configuration, separated by --. Either add to your existing garden.yaml file or create a second one, eg db.garden.yaml.

  • First module: persistent storage

    • Example code below. Adjust the storage size as applicable.

      1---
      2kind: Module
      3name: my-service-db-data
      4disabled: ${var.disabled-modules contains this.name || var.disabled-projects contains "my-service"}
      5type: persistentvolumeclaim
      6include: []
      7spec:
      8  accessModes: [ReadWriteOnce]
      9  resources:
      10    requests:
      11      storage: 5Gi
      
  • Second module: database container

    • type: container

    • For garden, we don't use hosted/managed databases but instead run the database engine from within a separate Kubernetes pod

    • Example code below. Make sure to replace my-service with your service name, #### with a new port number (you can choose randomly, eg using a port number generator).

      1---
      2kind: Module
      3type: container
      4name: my-service-db
      5disabled: ${var.disabled-modules contains this.name || var.disabled-projects contains "my-service"}
      6image: postgres:12.8-alpine
      7include: []
      8services:
      9  - name: my-service-db
      10    volumes:
      11      - name: data
      12        module: my-service-db-data
      13        containerPath: /var/lib/postgresql/data
      14    ports:
      15      - name: db
      16        containerPort: 5432
      17        localPort: ####
      18    env:
      19      PGDATA: /var/lib/postgresql/data/pgdata
      20      POSTGRES_DATABASE: postgres
      21      POSTGRES_USERNAME: postgres
      22      POSTGRES_PASSWORD: postgres
      

Accessing your database from the UI

Step 1: Download a postgres GUI, like pgAdmin or DataGrip

Step 2: Configure the server connection

  • Host: localhost
  • Get username, password and port (localPort) from garden.yaml
  • Save this configuration but do NOT connect just yet. (Uncheck the "Connect Now" option, if necessary)

Step 3: Run garden in dev mode, to do port forward

  • garden dev
    • As long as this is running, you should be able to connect to the db