我在Postgres容器上定义了多个环境变量,例如POSTGRES_USER.容器正在运行,我想使用exec从命令行连接到Postgres.
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.
我无法连接以下内容:
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>'我是否可以直接从docker exec执行psql命令并在容器上调用环境变量?
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> 推荐答案根据您的用例,您可以做什么,而不是将用户传递给 psql 命令,而是定义环境变量 PGUSER 到容器
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.
这样,如果您不指定任何用户,它将成为PostgreSQL的默认用户,因此您甚至不必指定它即可进行连接:
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=#更多推荐
Docker exec
发布评论