In CI: psql: Command not found

Hi everyone!
I’ve been trying to setup automatic tests of my app with cypress, but I ran into issues setting up the required database for my server. The CI below fails at the psql line. I already tried installing postgres via apt, but I didn’t get that running either, and I looked at the first few pages of Google results for this issue^^ I would be very grateful for help!

# Node docker image on which this would be run

cache:
  paths:
    - node_modules/

stages:
  - test
  - deploy_production

cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - node_modules/
    - .npm/

variables:
  POSTGRES_DB: nice_marmot
  POSTGRES_USER: runner
  POSTGRES_PASSWORD: "$password"
  POSTGRES_HOST_AUTH_METHOD: trust
  TEST_DB_URL: postgres://$POSTGRES_DB:$POSTGRES_PASSWORD@postgres:5432/$POSTGRES_DB

# Job 1:
Test:
  image: cypress/base:14.16.0
  stage: test
  services:
    - postgres:11
  script:
    - psql $TEST_DB_URL < db_schema.sql
    - npm install
    - npm run build
    - npm run test
  artifacts:
    when: always
    paths:
      - cypress/videos/**/*.mp4
      - cypress/screenshots/**/*.png
    expire_in: 1 day

# Job 2:
# Deploy to staging
Production:
  image: ruby:latest
  only:
    - master
  stage: deploy_production
  script:
    - apt-get update -qy
    - apt-get install -y ruby-dev
    - gem install dpl
    - dpl --provider=heroku --app=$HEROKU_APP_NAME --api-key=$HEROKU_API_KEY

In your Test job, add a postgres service; so something like

Test:
  image: cypress/base:14.16.0
  stage: test
  services:
    - postgres:11
  script:
    - psql $TEST_DB_URL < db_schema.sql
    - npm install
    - npm run build
    - npm run test
  artifacts:
    when: always
    paths:
      - cypress/videos/**/*.mp4
      - cypress/screenshots/**/*.png
    expire_in: 1 day

That should start a postgres11 docker container you can talk to via postgres

Thanks a lot! Sorry, I forgot to include the services line in my forum post, I already had that^^ I tried copying your suggestion directly to rule out spelling errors etc, but psql still wouldn’t work. (I edited the first post with the correct CI file).
The actual error message is as follows:

Executing "step_script" stage of the job script 00:01
Using docker image sha256:<removed> for cypress/base:14.16.0 with digest cypress/base@sha256:<removed> ...
$ export PGPASSWORD=$POSTGRES_PASSWORD
$ psql $C3_TEST_DB_URL < db_schema.sql
/bin/bash: line 130: psql: command not found
Uploading artifacts for failed job 00:01
Cleaning up file based variables 00:00
ERROR: Job failed: exit code 1

Is psql installed in the cypress/base:14.16.0 image? as that’s the environment those script commands are being executed in…

Ahh thanks, I didn’t read properly what services: does. No, by default postgres is not included (see cypress-docker-images/Dockerfile at master · cypress-io/cypress-docker-images · GitHub). I wasn’t able to install it in the script: part since apt doesn’t find it and adding the postgres repo didn’t work either.
My solution is that I execute the .sql file from within the app, which I don’t like but it works^^ If anyone has better solutions, feel free to share :slight_smile: