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

前端 未结 6 1297
孤独总比滥情好
孤独总比滥情好 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:13

    If you want to restore the database in a containder you can do this

    docker exec -i app_db_1 psql -U postgres < app_development.back
    

    Dont' forget to add -i

    :)

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-22 19:18

    This worked for me:

    goto bash :

    docker exec -it <container-name> bash
    

    from bash :

    psql -U <dataBaseUserName> <dataBaseName>
    

    or just this one-liner :

    docker exec -it  <container-name> psql -U <dataBaseUserName> <dataBaseName>
    

    helps ?

    0 讨论(0)
  • 2020-12-22 19:22

    If you have running "postgres" container:

    docker run -it --rm --link postgres:postgres postgres:9.6 sh -c "exec psql -h \$POSTGRES_PORT_5432_TCP_ADDR -p \$POSTGRES_PORT_5432_TCP_PORT -U postgres"
    
    0 讨论(0)
  • 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 <database username you want to connect with> -d <database name>
    

    Baammm!!!!! you are in.

    0 讨论(0)
  • 2020-12-22 19:32
    RUN /etc/init.d/postgresql start &&\
        psql --command "CREATE USER docker WITH SUPERUSER PASSWORD 'docker';" &&\
        createdb -O docker docker &&\
    
    0 讨论(0)
提交回复
热议问题