Run multiple PostgreSQL instances in Background steps
Use Background steps to run multiple PostgreSQL instances.
This topic explains how to use Background steps to run multiple PostgreSQL instances in a Harness CI pipeline.
You need:
A CI pipeline with a Build stage.
An understanding of PostgreSQL and the psql command.
Add PostgreSQL Background steps
Add one Background step for each PostgreSQL service you need to run.
For the Background steps to run the PostgreSQL services, the build environment must have the necessary binaries. Depending on the stage's build infrastructure, Background steps can use binaries that exist in the build environment or pull an image, such as a public or private Docker image, that contains the required binaries. For more information about when and how to specify images, go to the Background step Container Registry and Image settings.
Go to the Build stage in the pipeline where you want to add the Background steps.
On the Execution tab, select Add Step, and select the Background step from the Step Library.
Configure the Background step settings. Required settings depend on your build infrastructure.
Enter a Name and note the Id. You need the Id to call the service later in your pipeline.
Add the following Environment Variables:
POSTGRES_USER: Postgres database user name.POSTGRES_DB: Name for the database.POSTGRES_PASSWORD: Password for the specified user/database. You can use an expression to reference a Harness text secret.
Add a Port Binding, and then set the Host Post to
5432and the Container Port to the desired port for this database instance, such as5433.Configure other settings, such as Command, Entry Point, Container Registry, and Image, if needed.
Enter a Name and note the Id. You need the Id to call the service in later steps in this stage.
For Container Registry and Image, select a Docker connector and specify a PostgreSQL Docker image.
Add the following Entry Point arguments, and replace
PORT_NUMBERwith the desired port binding for this database instance, such as5433.docker-entrypoint.sh-p PORT_NUMBER
Add the following Environment Variables:
POSTGRES_USER: Postgres database user name.POSTGRES_DB: Name for the database.POSTGRES_PASSWORD: Password for the specified user/database. You can use an expression to reference a Harness text secret.
Configure other settings, if needed.
Select Apply Changes to save the step.
Add additional Background steps for each PostgreSQL instance you want to run. Make sure each instance has a different port binding. For example, if the first Background step was
5433, the second one could be5434.Arrange the Background steps to run in parallel.

In Harness, go to the pipeline where you want to run the PostgreSQL services. In the CI stage, add one Background step for each PostgreSQL database, and group the steps to run in parallel. Required Background step settings depend on your build infrastructure.
Test the PostgreSQL services
You can add a Run step to confirm your PostgreSQL services are running before running other steps that need to interact with those services.
For the Run step to run psql commands, the build environment must have the necessary binaries. Depending on the stage's build infrastructure, Run steps can use binaries that exist in the build environment or pull an image, such as a public or private Docker image, that contains the required binaries. For more information about when and how to specify images, go to the Run step Container Registry and Image settings.
In the same Build stage as your Background steps, add a Run step after (not in parallel with) your Background steps.
Configure the Run step settings. Required settings depend on your build infrastructure.
Enter a Name.
For Shell, select Sh.
Enter the following Commands and replace the placeholders with corresponding values from your Background steps.
For example:
Configure other settings, if needed.
Enter a Name.
For Container Registry and Image, select a Docker connector and specify a PostgreSQL Docker image.
For Shell, select Sh.
Enter the following Commands and replace the placeholders with corresponding values from your Background steps.
For example:
Configure other settings, if needed.
Select Apply Changes to save the step.
Run your pipeline to test your PostgreSQL services. You can monitor and review build logs on the Build details page. Once you've confirmed that the services run as expected, you can configure the rest of your pipeline. Note that Background steps don't persist across stages.
In the same CI stage where you added the Background steps, add a Run step after the Background steps. Make sure the Run step isn't in the -parallel group. Required Run step settings depend on your build infrastructure.
In command, you must provide values from your Background steps:
In command, you must provide values from your Background steps:
After adding the Runstep, run your pipeline to test your PostgreSQL services. You can monitor and review build logs on the Build details page. Once you've confirmed that the services run as expected, you can configure the rest of your pipeline. Note that Background steps don't persist across stages.
Pipeline YAML examples
Troubleshooting: Failed to get image entrypoint
If you get a failed to get image entrypoint error when using a Kubernetes cluster build infrastructure, you might need to mount volumes for the PostgreSQL data and then reference those volumes in the Background steps.
In the build infrastructure settings, mount one empty directory volume for each PostgreSQL service.
In each PostgreSQL Background step, add a
PGDATAenvironment variable, and set the value to the corresponding empty directory path.
Last updated
Was this helpful?