Home > Software design >  psql command from official page on dockerhub
psql command from official page on dockerhub

Time:11-24

I am learning Docker. I have practiced a lot, including testing commands from the official Postgres page on dockerhub.

I ran this command:

docker run -it --rm --network some-network postgres psql -h some-postgres -U postgres 

Could someone give a complete and concrete example to make this command work (i mean with a real existing container). I can't see how it could work.

CodePudding user response:

docker run create a docker container
-it create a connection to said container (kinda like TTY) taking in what we write into interactive bash in the container
--rm delete the container when it exit
--network some-network assign some-network network to the container
postgres name of the image
psql -h some-postgres -U postgres connect to PostgreSQL at some-postgres address using postgres user.

Combine the entire command and flags: create a PostgreSQL container and the use the psql command from inside the container to connect to some-postgres using postgres user
For more flags and usage, you can learning from the doc here

CodePudding user response:

Probably, in the Docker hub page is not perfectly clear but your command is used to connect to an already existing Postgres instance.

So, for example, you first create a container with the command:

docker run -it --rm --name postgresql -p 5432:5432 -e POSTGRES_USER=admin -e POSTGRES_PASSWORD=admin -d postgres:latest

then you can execute your command to connet to it

docker run -it --rm postgres psql -h <your_ip> -U postgres

If your container is running locally, you can get the ip from the bash command ip address

The network attibute is related to the container you first startup so you can decide to leave or remove from the command in relation to the container deploy.

  • Related