Using docker

lonewanderer@debian:~sudo docker exec -it <CONTAINER_NAME> postgres -V

Using docker compose

lonewanderer@debian:~ sudo docker compose exec -t db postgres -V db is the name of your container as defined in your docker-compose.yml file.

Output

The output will look somthing like this:

postgres (PostgreSQL) 16.1 (Debian 16.1-1.pgdg120+1)