Docker Community Forums

Share and learn in the Docker community.

Gitlab CI/CD Postgres connection

Type of build:

Technically, this is a Docker-in-Docker type of scenario. Think of it like a host container with a database container inside of it.

The command “docker run --name pgdock -d -p 5432:5432 postgres:12.1” was executed, exposing ports.


Command/commands to:

Connect to postgres container from host (a container defined by Gitlab CI)

Display all databases to ensure they were built properly (display them inside host/host container to confirm)

All without having to have any manual input aside from the command itself


I tried docker exec -it pgdock etc. etc. but had a TTY issue with the -t flag (because it’s not very interactive in a pipeline)

I ran this command to get access to the container from host:
Docker exec pgdock bash -c "psql -U postgres; /l

result: /l wasn’t found in bash. I figured the first command in the list would log into psql and then I could execute /l to display all databases.

Docker exec pgdock bash -c "psql -U postgres" does work and most other forms of this command (using -i flag etc.)

Thanks for reading everyone, I hope this is digestible–as this is my first post!

This command does not make sense. It is is incomplete and the syntax is wrong. The ; character is a line terminator in bash as well, so /l byitself won’t be found as a command and raise an error.

If you pass the sql statment to psql, only a single statement will be allowed.
If you pass a here document to the psql binary, you can pass multiple statements:

docker exec pgdock psql -U postgres << EOF
  SELECT * from FOO;

EOF is the start/stop marker of the here doc content and << is the ridirect to that command. You can use whatever here doc marker you like instead of EOF, as long as it is in caps and does not appear somewhere else in the content.