麒麟信创系统安装pgsql-15.4 报错 popen failure: Cannot allocate memory

[root@iZbp1jchymfq9318t02m5wZ pgsql]# docker-compose up
[+] Running 11/14
[+] Running 14/14⣿⣿⣿⣿⣿⣿⣿⣿⠀⣿⣿⣿⠀] Pulling                                                                                                             15.9
 ✔ pgsql-15.4 Pulled                                                                                                                              42.5s 
   ✔ 578acb154839 Pull complete                                                                                                                    7.9s 
   ✔ 9abc159edb5f Pull complete                                                                                                                    8.0s 
   ✔ b742e0d9e952 Pull complete                                                                                                                    8.2s 
   ✔ 8d6c67b42441 Pull complete                                                                                                                    8.3s 
   ✔ 4ddc08bac214 Pull complete                                                                                                                    8.7s 
   ✔ cf5a0484b6d9 Pull complete                                                                                                                    8.8s 
   ✔ 1b4a0642b63d Pull complete                                                                                                                    8.9s 
   ✔ 2f2829f664a0 Pull complete                                                                                                                    9.0s 
   ✔ 7877975bfbf0 Pull complete                                                                                                                   41.8s 
   ✔ e4b736138f60 Pull complete                                                                                                                   41.9s 
   ✔ 2ce63fdb79b4 Pull complete                                                                                                                   41.9s 
   ✔ 5734338d16e4 Pull complete                                                                                                                   42.0s 
   ✔ 0a84e9353ffb Pull complete                                                                                                                   42.1s 
[+] Running 1/1
 ✔ Container pgsql-15.4  Created                                                                                                                   1.6s 
Attaching to pgsql-15.4
pgsql-15.4  | popen failure: Cannot allocate memory
pgsql-15.4  | initdb: error: program "postgres" is needed by initdb but was not found in the same directory as "/usr/lib/postgresql/15/bin/initdb"
pgsql-15.4 exited with code 1
pgsql-15.4  | popen failure: Cannot allocate memory
pgsql-15.4  | initdb: error: program "postgres" is needed by initdb but was not found in the same directory as "/usr/lib/postgresql/15/bin/initdb"
pgsql-15.4  | popen failure: Cannot allocate memory
pgsql-15.4  | initdb: error: program "postgres" is needed by initdb but was not found in the same directory as "/usr/lib/postgresql/15/bin/initdb"
pgsql-15.4  | popen failure: Cannot allocate memory
pgsql-15.4  | initdb: error: program "postgres" is needed by initdb but was not found in the same directory as "/usr/lib/postgresql/15/bin/initdb"
pgsql-15.4  | popen failure: Cannot allocate memory
pgsql-15.4  | initdb: error: program "postgres" is needed by initdb but was not found in the same directory as "/usr/lib/postgresql/15/bin/initdb"
pgsql-15.4  | popen failure: Cannot allocate memory
pgsql-15.4  | initdb: error: program "postgres" is needed by initdb but was not found in the same directory as "/usr/lib/postgresql/15/bin/initdb"
pgsql-15.4  | popen failure: Cannot allocate memory
pgsql-15.4  | initdb: error: program "postgres" is needed by initdb but was not found in the same directory as "/usr/lib/postgresql/15/bin/initdb"

1、创建诊断脚本

cat > diagnose_docker_postgres.sh << 'EOF'
#!/bin/bash

echo "======================================"
echo "PostgreSQL Docker 问题诊断脚本"
echo "======================================"
echo ""

# 1. 系统基本信息
echo "【1. 系统信息】"
echo "内核版本: $(uname -r)"
echo "系统版本: $(cat /etc/redhat-release 2>/dev/null || cat /etc/os-release | grep PRETTY_NAME)"
echo "CPU核心数: $(nproc)"
free -h
echo ""

# 2. 内存相关参数
echo "【2. 内存相关内核参数】"
echo "overcommit_memory: $(cat /proc/sys/vm/overcommit_memory)"
echo "overcommit_ratio: $(cat /proc/sys/vm/overcommit_ratio)"
echo "max_map_count: $(cat /proc/sys/vm/max_map_count)"
echo "swappiness: $(cat /proc/sys/vm/swappiness)"
echo ""

# 3. Docker 信息
echo "【3. Docker 信息】"
docker version
echo ""
docker info | grep -E "Storage Driver|Cgroup Driver|Cgroup Version|Operating System|Kernel Version"
echo ""

# 4. 检查 cgroup 限制
echo "【4. Cgroup 限制检查】"
if [ -d /sys/fs/cgroup/memory/docker ]; then
    echo "Memory cgroup 配置:"
    cat /sys/fs/cgroup/memory/docker/memory.limit_in_bytes 2>/dev/null || echo "无法读取"
    cat /sys/fs/cgroup/memory/docker/memory.memsw.limit_in_bytes 2>/dev/null || echo "无法读取"
fi
echo ""

# 5. 检查 Docker 存储
echo "【5. Docker 存储空间】"
df -h | grep -E "Filesystem|/var/lib/docker|overlay"
echo ""
docker system df
echo ""

# 6. 测试容器内存分配
echo "【6. 测试简单容器运行】"
echo "运行测试容器..."
docker run --rm alpine sh -c "echo '容器可以正常运行'; free -m" 2>&1
echo ""

# 7. 测试 PostgreSQL 容器内部
echo "【7. 测试 PostgreSQL 镜像内部】"
echo "检查 postgres 程序是否存在..."
docker run --rm postgres:15.4 sh -c "ls -la /usr/lib/postgresql/15/bin/postgres 2>&1; which postgres 2>&1"
echo ""

# 8. 测试内存限制的容器
echo "【8. 测试带内存限制的 PostgreSQL】"
echo "尝试以 512MB 内存限制运行..."
timeout 10 docker run --rm -m 512m \
  -e POSTGRES_PASSWORD=test123 \
  postgres:15.4 2>&1 | head -20
echo ""

# 9. 检查 SELinux
echo "【9. SELinux 状态】"
getenforce 2>/dev/null || echo "SELinux 未安装"
echo ""

# 10. 检查资源限制
echo "【10. 系统资源限制】"
ulimit -a
echo ""

# 11. 检查 dmesg 错误
echo "【11. 最近的内核错误(OOM/内存相关)】"
dmesg | grep -i -E "out of memory|oom|kill|memory" | tail -20
echo ""

# 12. 尝试修复建议
echo "======================================"
echo "【尝试以下修复方案】"
echo "======================================"
echo ""
echo "方案1: 调整内核参数"
echo "----------------------------------------"
echo "sudo sysctl -w vm.overcommit_memory=1"
echo "sudo sysctl -w vm.max_map_count=262144"
echo ""
echo "方案2: 修改 docker-compose.yml 添加配置"
echo "----------------------------------------"
cat << 'COMPOSE'
services:
  pgsql-15.4:
    container_name: pgsql-15.4
    image: postgres:15.4
    restart: always
    ports:
      - "5432:5432"
    environment:
      TZ: Asia/Shanghai
      POSTGRES_USER: pgvector
      POSTGRES_PASSWORD: bCK27cNKp4Ypafi75
    volumes:
      - ./data:/var/lib/postgresql/data
    # 添加以下配置
    shm_size: '256mb'
    security_opt:
      - seccomp:unconfined
    tmpfs:
      - /tmp
      - /run
networks:
  default:
    external: true
    name: shgbitai-network
COMPOSE
echo ""
echo "方案3: 不使用 volume,测试是否是权限问题"
echo "----------------------------------------"
echo "docker run -d --name test-pg -e POSTGRES_PASSWORD=test123 postgres:15.4"
echo ""

EOF

添加执行权限

chmod +x diagnose_docker_postgres.sh

运行诊断

bash./diagnose_docker_postgres.sh > diagnostic_report.txt 2>&1
cat diagnostic_report.txt

同时,立即尝试这个快速测试

# 测试1: 不挂载 volume,直接运行
echo "测试1: 不使用 volume"
docker run -d --name test-pg-no-volume \
  -e POSTGRES_PASSWORD=test123 \
  postgres:15.4

sleep 5
docker logs test-pg-no-volume
docker rm -f test-pg-no-volume

echo ""
echo "================================"
echo ""

# 测试2: 调整内核参数后运行
echo "测试2: 调整内核参数"
sysctl -w vm.overcommit_memory=1
sysctl -w vm.max_map_count=262144

# 修改 docker-compose.yml
cat > docker-compose-test.yml << 'EOF'
services:
  pgsql-15.4:
    container_name: pgsql-15.4-test
    image: postgres:15.4
    restart: "no"
    ports:
      - "5432:5432"
    environment:
      TZ: Asia/Shanghai
      POSTGRES_USER: pgvector
      POSTGRES_PASSWORD: bCK27cNKp4Ypafi75
    volumes:
      - ./data:/var/lib/postgresql/data
    shm_size: '256mb'
    security_opt:
      - seccomp:unconfined
networks:
  default:
    external: true
    name: shgbitai-network
EOF
docker-compose -f docker-compose-test.yml up
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MonkeyKing.sun

对你有帮助的话,可以打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值