30

I'm looking for a Docker image that would only have all the necessary components to make calls to an external Postgres database using the psql client in the shell. I do not need to launch a database locally or anything.

I found jbergknoff/postgresql-client, which I haven't tested, but I'm just mostly surprised there doesn't seem to be any official image for that.

For now I'm using postgres:12-alpine, but it's only as part of a CronJob launched in Kubernetes which takes care of triggering a clean up of certain tables every once in a while by calling a Postgres function.

Anyone has something to recommend? Or some insight to share?

3
  • Building an image FROM ubuntu with a single RUN apt-get update && apt-get install command wouldn't be hard. Mostly, though, it's easier to run interactive tools like psql directly from the host than to get them packaged in containers and sudo docker run --rm -it registry.example.com/psql for this kind of task.
    – David Maze
    Commented Dec 15, 2021 at 12:12
  • If all you need is to run certain queries periodically you can take a look at pg_cron which does exactly that.
    – SebDieBln
    Commented Dec 15, 2021 at 13:08
  • 2
    Precision: we use a managed Postgres server from Azure, which thus effectively means we cannot execute commands from within the host itself, nor can we use pg_cron because it isn't supported.
    – payne
    Commented Dec 15, 2021 at 15:21

2 Answers 2

27

IMHO, It's actually better to create our own Docker image instead of relying on some third party for such trivial need. Supply chain security is already tricky enough.

Here is the content of my docker file PgClientDockerfile:

FROM alpine:3.15
RUN apk --no-cache add postgresql12-client
ENTRYPOINT [ "psql" ]

Here is an excerpt from my docker-compose.yml:

version: '3.7'
services:
  pg_client:
    environment:
      PGDATABASE: ${pg_database}
      PGHOST: ${pg_host}
      PGPORT: ${pg_port}
      PGUSER: ${pg_user}
      PGPASSWORD: ${pg_password}
    build:
      context: .
      dockerfile: PgClientDockerfile

I have an .env file with values like:

pg_database=mydb
pg_host=mydbhost
pg_port=5432
pg_user=myuser
pg_password=mypassword

And here is an example usage (for my use case):

#!/usr/bin/env bash

docker-compose run --rm pg_client -c 'drop trigger if exists my_trigger on my_table RESTRICT'
docker-compose run --rm another_docker_service ./run_batch_insert.sh
docker-compose run --rm pg_client -c 'create trigger my_trigger after insert or update or delete or truncate on my_table for each statement execute procedure refresh_my_table_finder()'

4

I created an image for the exact purpose, only have the psql client. Here is the image on DockerHub https://hub.docker.com/r/codingpuss/postgres-client

Simply create a container

docker run -dit --network=local_net --name=pgclient codingpuss/postgres-client

Then execute the command as you like:

docker exec -it pgclient psql postgresql://postgres:1@pgdb:5432/postgres -c 'select * from pg_catalog.pg_tables'
1
  • The "Start the container" example in the Docker webpage is bogus, as it contains a postgresql URL in-mid docker command which is syntactically invalid. Commented Apr 3 at 9:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.