3

I have multiple Environment Variables defined on my Postgres container, such as POSTGRES_USER. The container is running and I want to connect to Postgres from the command line using exec.

I'm unable to connect with the following:

docker exec -it <CONTAINER-ID> psql -U $POSTGRES_USER -d <DB NAME>

I understand that the variable is defined on the container and the following does work:

docker exec -it <CONTAINER-ID> bash -c 'psql -U $POSTGRES_USER  -d <DB NAME>'

Is there a way for me to execute the psql command directly from docker exec and call the environment variable on the container?

docker exec -it <CONTAINER-ID> psql -U ????? -d <DB NAME>
6
  • 1
    '$POSTGRES_USER' Commented Aug 19, 2020 at 18:30
  • docker exec -it <docker_container_name> /bin/bash and then psql -d <postgres_db_name> -U <postgres_username> Commented Aug 19, 2020 at 19:37
  • Thanks @MikeOrganek, but this is still causing an error. I think the container picks this up as a string. psql: error: could not connect to server: FATAL: role "$POSTGRES_USER" does not exist Commented Aug 19, 2020 at 19:45
  • Thanks @dassum, I edited my question to include the db name but this doesn't address the issue of using environment variables. Commented Aug 19, 2020 at 19:46
  • Sorry about that. That is really messed up. The only way I can get it to work right now is sh -c 'psql -U $POSTGRES_USER <dbname>'. I think it is necessary to pass it to sh for the environment variable to be evaluated. If I find another way I will comment here. Commented Aug 19, 2020 at 19:55

2 Answers 2

2

Depending on your use case, what you could do, instead of passing a user to the psql command is to define the environment variable PGUSER to the container at boot time.

This way, it will be the default user for PostgreSQL, if you do not specify any, so you won't even have to specify it in order to connect:

$ docker run --name postgres -e POSTGRES_PASSWORD=bar -e POSTGRES_USER=foo -e PGUSER=foo -d postgres
e250f0821613a5e2021e94772a732f299874fc7a16b340ada4233afe73744423

$ docker exec -ti postgres psql -d postgres                                                          
psql (12.4 (Debian 12.4-1.pgdg100+1))
Type "help" for help.

postgres=# 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I set PGUSER on the container at boot and this worked.
0

The reason this isn't working for you is because when you run the command

docker exec -it <CONTAINER-ID> psql -U $POSTGRES_USER -d <DB NAME>

You're running it on your host. So, $POSTGRES_USER refers to the environment variable on your host, not your container. That variable isn't set on your host.

The second command

docker exec -it <CONTAINER-ID> bash -c 'psql -U $POSTGRES_USER  -d <DB NAME>'

works because you're passing the command in the quotes to the shell in the container, where that variable actually exists.

The method in the second command is the way to do what you're trying to do, unless you set the variable on your host somehow and make sure it has the same value as it does in your container.

The easiest way to do this would be to reference your host variable at image build time.

So, in your Dockerfile, if you write ENV POSTGRES_USER=${POSTGRES_USER} it will look in the host environment for that value, and use it.

If you set the variables this way, then your command will work.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.