Ubuntu 22.02 Docker环境安装PostgreSQL 15报错求助
Docker Ubuntu 22.04 安装PostgreSQL 15失败问题排查修复
错误根源
构建脚本中添加PostgreSQL官方源的命令存在语法错误:$(echo lsb_release -cs) 会直接输出字符串 "lsb_release -cs",而非执行lsb_release -cs命令获取Ubuntu 22.04的codename(jammy),导致生成的源地址为https://apt.postgresql.org/pub/repos/apt lsb_release-pgdg main,该地址无效,触发404错误。
修复方案
方案1:修正命令,正确获取系统codename
先确保安装lsb-release包(用于执行lsb_release -cs),再修正源添加命令:
RUN apt update && apt install wget gnupg gnupg2 gnupg1 lsb-release -y RUN sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' RUN wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - RUN apt update && apt-get install -y postgresql-15 postgresql-client-15
方案2:直接写死Ubuntu 22.04的codename(更稳定)
Ubuntu 22.04的codename是jammy,直接替换变量,无需依赖lsb-release:
RUN apt update && apt install wget gnupg gnupg2 gnupg1 -y RUN sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt jammy-pgdg main" > /etc/apt/sources.list.d/pgdg.list' RUN wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - RUN apt update && apt-get install -y postgresql-15 postgresql-client-15
补充说明
- 若后续使用其他Ubuntu版本,只需将方案2中的
jammy替换为对应版本的codename即可(如20.04是focal)。 apt-key add方式已被标记为过时,若需更规范的做法,可改用gpg --dearmor将密钥存入/usr/share/keyrings/目录,不过当前方案仍可正常工作。
内容的提问来源于stack exchange,提问作者Flori00312
相关产品推荐
相关产品推荐

