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

前端 未结 6 1352
孤独总比滥情好
孤独总比滥情好 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条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-22 19:15

    docker exec -it yiialkalmi_postgres_1 psql -U project -W project project
    

    Some explanation

    • docker exec -it The command to run a command to a running container. The it flags open an interactive tty. Basically it will cause to attach to the terminal. If you wanted to open the bash terminal you can do this

    docker exec -it yiialkalmi_postgres_1 bash

    • yiialkalmi_postgres_1 The container name (you could use the container id instead, which in your case would be 40e39bd0329a)
    • psql -U project -W project The command to execute to the running container

    • U user

    • W password
    • project the database you want to connect to.

    These are specified by you here

    environment:
        POSTGRES_DB: project
        POSTGRES_USER: project
        POSTGRES_PASSWORD: project
    

提交回复
热议问题