How to execute psql command in postgres docker build




My settings looks similar to this post.

  - image: circleci/python:3.6.1
  - image: mdillon/postgis:9.6
      POSTGRES_USER: circleci
      POSTGRES_DB: circle_test

'default': {
    'ENGINE': 'django.contrib.gis.db.backends.postgis',
    'NAME': 'circle_test',
    'HOST': '',
    'PORT': '5432',
    'USERNAME': 'circleci',
    'PASSWORD': ''

I also wanted the below a psql command:

sudo -u circleci psql -h localhost -U circleci -d circle_test -f materialized_views.sql

I get a this error

bash: psql: command not found.

Any thoughts on the best way to exeucte the psql commands?

Django, PostgreSQL and CircleCI
NPM can't be found by circle

Are you trying to execute this command within the python or the postgres/postgis instance ? I think it’s not available in the python instance.


To follow up on what @jibaku said, your specific issues is that the Postgres client software (psql) is not installed by default in circleci/python Docker images. That’s normal, since installing it by default would bloat the image size for something only a subset of the users need.

The circleci/python are based on the official Docker images using the Debian Jessie base. This leaves you with two options if you need psql installed. First, you can install psql as part of your CircleCI set-up using a run step:

- run: apt-get install -y postgresql-client

This can add some unnecessary time to your build, though. Instead, I would recommend you build a custom Docker image with exactly the tooling you need. You can even use the CircleCI image as a base:

FROM circleci/python:3.6.1
RUN apt-get update && apt-get install -y postgresql-client
CMD ["python3"]

You can find more details about building custom images on the documentation page “Using Custom-Built Docker Images”.

An additional point: you do not need to use sudo here. The Docker image has already set the user to circleci. Even if it did not, executing under the circleci UID would not provide much benefit for this command.


This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.