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

PostgresqlDockerDocker Compose

Postgresql Problem Overview


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 the psql command is not found...

For me a little bit it is a mystery how I can run postgre sql queries or commands in the container.

How run the psql command in the postgres container? (I am a new guy in Docker world)

I use Ubuntu as a host machine, and I did not install the postgres on the host machine, I use the postgres container instead.

docker-compose ps
        Name                       Command               State               Ports            
---------------------------------------------------------------------------------------------
yiialkalmi_app_1        /bin/bash                        Exit 0                               
yiialkalmi_nginx_1      nginx -g daemon off;             Up       443/tcp, 0.0.0.0:80->80/tcp 
yiialkalmi_php_1        php-fpm                          Up       9000/tcp                    
yiialkalmi_postgres_1   /docker-entrypoint.sh postgres   Up       5432/tcp                    
yiialkalmi_redis_1      docker-entrypoint.sh redis ...   Up       6379/tcp     

Here the containers:

docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                         NAMES
315567db2dff        yiialkalmi_nginx    "nginx -g 'daemon off"   18 hours ago        Up 3 hours          0.0.0.0:80->80/tcp, 443/tcp   yiialkalmi_nginx_1
53577722df71        yiialkalmi_php      "php-fpm"                18 hours ago        Up 3 hours          9000/tcp                      yiialkalmi_php_1
40e39bd0329a        postgres:latest     "/docker-entrypoint.s"   18 hours ago        Up 3 hours          5432/tcp                      yiialkalmi_postgres_1
5cc47477b72d        redis:latest        "docker-entrypoint.sh"   19 hours ago        Up 3 hours          6379/tcp                      yiialkalmi_redis_1

And this is my docker-compose.yml:

app:
image: ubuntu:16.04
volumes:
    - .:/var/www/html

nginx:
    build: ./docker/nginx/
    ports:
        - 80:80
    links:
        - php
    volumes_from:
        - app
    volumes:
        - ./docker/nginx/conf.d:/etc/nginx/conf.d

php:
    build: ./docker/php/
    expose:
        - 9000
    links:
        - postgres
        - redis
    volumes_from:
        - app

postgres:
    image: postgres:latest
    volumes:
        - /var/lib/postgres
    environment:
        POSTGRES_DB: project
        POSTGRES_USER: project
        POSTGRES_PASSWORD: project

redis:
    image: redis:latest
    expose:
        - 6379

Postgresql Solutions


Solution 1 - Postgresql

docker exec -it yiialkalmi_postgres_1 psql -U project -W 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 Tell psql that the user needs to be prompted for the password at connection time. This parameter is optional. Without this parameter, there is an extra connection attempt which will usually find out that a password is needed, see the PostgreSQL docs.

  • project the database you want to connect to. There is no need for the -d parameter to mark it as the dbname when it is the first non-option argument, see the docs: -d "is equivalent to specifying dbname as the first non-option argument on the command line."

These are specified by you here

environment:
    POSTGRES_DB: project
    POSTGRES_USER: project
    POSTGRES_PASSWORD: project

Solution 2 - Postgresql

If you need to restore the database in a container you can do this:

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

Don't forget to add -i.

:)

Solution 3 - Postgresql

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 ?

Solution 4 - Postgresql

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.

Solution 5 - Postgresql

After the Postgres container is configured using docker, open the bash terminal using:

docker exec -it <containerID>(postgres container name / ID) bash

Switch to the Postgres user:

su - postgres

Then run:

psql

It will open the terminal access for the Postgres.

Solution 6 - Postgresql

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"

Solution 7 - Postgresql

We can enter the container with a terminal sh or bash by using,

> docker run -it

if assume it is sh,

> psql -U postgres

will work

Solution 8 - Postgresql

RUN /etc/init.d/postgresql start &&\
    psql --command "CREATE USER docker WITH SUPERUSER PASSWORD 'docker';" &&\
    createdb -O docker docker &&\

Solution 9 - Postgresql

Just fired up a local test, not sure if -c is what you were after from the cli.

docker run -it --rm --name psql-test-connection -e PGPASSWORD=1234 postgres psql -h kubernetes.docker.internal -U awx -c "\conninfo"

You are connected to database "awx" as user "awx" on host "kubernetes.docker.internal" (address "192.168.65.4") at port "5432".

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDabagabView Question on Stackoverflow
Solution 1 - PostgresqlAlkis KalogerisView Answer on Stackoverflow
Solution 2 - PostgresqljoseloView Answer on Stackoverflow
Solution 3 - PostgresqlAlferd NobelView Answer on Stackoverflow
Solution 4 - PostgresqlthiernoView Answer on Stackoverflow
Solution 5 - PostgresqlChinnuView Answer on Stackoverflow
Solution 6 - PostgresqlVojtech VitekView Answer on Stackoverflow
Solution 7 - PostgresqlNuwaView Answer on Stackoverflow
Solution 8 - PostgresqlvijayView Answer on Stackoverflow
Solution 9 - Postgresqlb0buView Answer on Stackoverflow