pgcli container for postgres with password urlencoding
Run a container with the postgres connection string.
docker run --rm -it --network some-network devpies/pgcli postgres://user:pass@host:port/database# Create network
docker network create some-network
# Run postgres
docker run --name pg-db --detach --network some-network -e POSTGRES_PASSWORD=secret -p 5432:5432 postgres:18-alpine
# Run devpies/pgcli
docker run --rm --network some-network -it devpies/pgcli postgres://postgres:secret@pg-db:5432/postgres # Run postgres
docker run --network host -d -e POSTGRES_PASSWORD=secret postgres:18-alpine
# Run devpies/pgcli
docker run --rm -it --network host devpies/pgcli postgres://postgres:secret@localhost:5432/postgres
# Or use Docker Desktop's "host.docker.internal"# Run postgres
docker run -d -e POSTGRES_PASSWORD=secret -p 5432:5432 postgres:18-alpine
# Run devpies/pgcli
docker run --rm -it devpies/pgcli postgres://postgres:secret@host.docker.internal:5432/postgresdevpies/pgcli can handle passwords in both encoded and unencoded forms, reducing the risk of connection errors due to improper encoding.
The image first tries to connect using the password as provided, assuming it doesn’t need encoding. This avoids unnecessary encoding when it’s not needed.
If the initial connection fails, the image then URL-encodes the password and retries the connection, ensuring that special characters in the password are correctly handled.
Add a service to your project's compose file and place it on the same network as the postgres container. Provide the postgres connection string as an environment variable: CONN.
services:
pgcli:
image: devpies/pgclienvironment:
CONN: $CONNnetworks:
- dbaccessThen run:
docker compose run --rm pgcli
Note: You might notice with docker compose up, pgcli starts alongside your other services but exits immediately. This is normal. Containers exit when they don't have a running process to keep it alive.
This project is licensed under the MIT License. See the LICENSE file for details.