Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres lean Docker image containing only "psql" client

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?

like image 715
payne Avatar asked Aug 31 '25 18:08

payne


2 Answers

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()'

like image 128
rijam Avatar answered Sep 02 '25 15:09

rijam


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'
like image 25
Dũng Trần Trung Avatar answered Sep 02 '25 14:09

Dũng Trần Trung