Step 4: Configure a database

Our application looks good, as we can now create events and we have a frontend and backend automatically running when we start workspaces. However, our APIs won’t work yet, as they need to be configured to use a database.

With Gitpod you can connect to externally hosted databases. However, for simplicity, in this tutorial we’re going to set up a database that runs in our workspace. To help installing our database we’re going to use Gitpod workspace iamges.

Understanding workspace images

When no image is referenced in the .gitpod.yml workspaces use the workspace-full image by default, which comes with many commonly used utilities. If you want to override the workspace image you can reference a built Docker image.

.gitpod.yml
image: registry.hub.docker.com/your_username/image

Or, you can reference a Dockerfile in the repository.

.gitpod.yml
image:
    file: .gitpod.Dockerfile

Installing Postgres

For our example we’ll use Postgres with the Gitpod PostgresQL workspace image.

Copy the following line into your .gitpod.yml file:

.gitpod.yml
image: gitpod/workspace-postgres

The image will start postgres by default on port 5432. Let’s add a port configuration to define and ignore that port in our .gitpod.yml.

Again, copy the following lines into your .gitpod.yml file:

.gitpod.yml
 - name: database
   port: 5432
   onOpen: ignore

Database seeding

Finally, let’s automate the creation of our schema, and any necessary data seeding. For that, let’s add our init-db.sh script to our .gitpod.yml. Copy the following into the tasks block that we created earlier:

.gitpod.yml
 - name: create tables in db
   command: './init-db.sh'

Next Steps

Configure environment variables →

Was this helpful?