【问题标题】:Postgres database running in docker keeps hanging在 docker 中运行的 Postgres 数据库一直挂起
【发布时间】:2020-10-20 13:03:55
【问题描述】:

我正在使用 postgres docker 映像,并且在使用在 docker 映像中运行的数据库数月之后,现在我得到的行为是,在一段时间后,它们只是挂起。我可以使用bin/bash 执行,但对 postgres 根本无能为力;命令不返回,容器不能被放下。即使docker kill -s SIGKILL <container_id> 也不起作用;需要重新启动 docker 服务器才能停止它们。

我能看到的唯一确凿证据是消息:

 WARNING:  could not open statistics file "pg_stat_tmp/global.stat": Operation not permitted

在所有容器上。任何人有任何想法我都会非常感激,因为目前这正在扼杀一切。

【问题讨论】:

  • 谁在使用 postgres(我猜你有某种后端连接到它......)。你处理什么样的数据。您是否有任何大型或复杂的查询可能会导致 postgres 没有内存或服务器没有空间?
  • 这实际上是在我的笔记本电脑上。没有大型或复杂的查询。没有太多数据。还剩很多内存。
  • 自从升级到最新版本的 docker 后,我的开发环境也遇到了同样的问题
  • 我已经将 docker 从 2.5 降级到 2.4,到目前为止它变得更好..
  • FWIW 我在 docker 上的 Postgres 日志中经常收到这些相同的警告,但在我的情况下似乎不会导致任何挂起。 PostgreSQL 9.6.15,docker 引擎 19.03.13,macOS High Sierra。

标签: postgresql docker


【解决方案1】:

这是由于 docker 容器中的用户权限不匹配而发生的。

列出容器中的相关文件:

$ docker exec <container> ls -l /var/lib/postgresql/data/pg_stat_tmp
-rw------- 1 root     root     [...] db_0.stat
-rw------- 1 root     root     [...] db_1.stat
-rw------- 1 root     root     [...] db_2.stat
-rw------- 1 postgres postgres [...] global.stat

我们可以看到所有db_*.stat文件都归root:root所有,而global.statpostgres:postgres所有。

检查 docker 用户给了我们:

$ docker exec <container> whoami
root

因此,我们希望所有这些文件都归 postgres 用户所有。 幸运的是,这很容易!只需将用户设置为postgres,然后重新启动!

在 dockerfile 中:

USER postgres

使用 docker-compose:

services:
  postgres:
    image: postgres:13
    user: postgres

【讨论】:

  • 不适合我。 Postgres数据目录下的所有文件或目录都属于postgres:postgres,但docker logs -f postgres的日志仍然弹出``` WARNING: could not open statistics file "pg_stat_tmp/global.stat": Operation not allowed ```跨度>
【解决方案2】:

由于 Norling 和 Dharman 的回答对我不起作用,我尝试了另一种方法:将临时文件留在容器中 - 这很有效。我已经使用内联命令更改了 Postgresql 配置:

postgresdb: 
    image: 'postgres'
    command: postgres -c stats_temp_directory=/tmp
    .
    .
    .

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-30
    • 2020-02-20
    • 2019-05-18
    • 1970-01-01
    • 2021-09-19
    • 2020-10-29
    相关资源
    最近更新 更多