Docker - How can run the psql command in the postgres container?

前端 未结 6 1339
孤独总比滥情好
孤独总比滥情好 2020-12-22 18:44

I would like to use the psql in the postgres image in order to run some queries on the database. But unfortunately when I attach to the postgres container, I got that error

6条回答
  •  Happy的楠姐
    2020-12-22 19:27

    You can enter inside the postgres container using docker-compose by typing the following

    docker-compose exec postgres bash
    

    knowing that postgres is the name of the service. Replace it with the name of the Postgresql service in you docker-compose file.

    if you have many docker-compose files, you have to add the specific docker-compose.yml file you want to execute the command with. Use the following commnand instead.

    docker-compose -f < specific docker-compose.yml> exec postgres bash
    

    For example if you want to run the command with a docker-compose file called local.yml, here the command will be

    docker-compose -f local.yml exec postgres bash
    

    Then, use psql command and specify the database name with the -d flag and the username with the -U flag

    psql -U  -d 
    

    Baammm!!!!! you are in.

提交回复
热议问题