温馨提示×

Ubuntu pgAdmin怎么配置

小樊
37
2026-08-21 12:47:03
栏目: 智能运维

Ubuntu 上配置 pgAdmin(以 pgAdmin 4 为例,目前主流版本)通常分为 安装 → 初始化 → 连接 PostgreSQL 三步。下面给你一套最常用、最稳妥的做法(适用于 Ubuntu 20.04 / 22.04 / 24.04)。


一、安装 PostgreSQL(如果还没装)

sudo apt update
sudo apt install postgresql postgresql-contrib

检查状态:

sudo systemctl status postgresql

二、安装 pgAdmin 4(推荐方式:APT 官方仓库)

1️⃣ 添加 pgAdmin 官方仓库

curl -fsSL https://www.pgadmin.org/static/packages_pgadmin_org.pub | sudo gpg --dearmor -o /usr/share/keyrings/pgadmin-archive-keyring.gpg

echo "deb [signed-by=/usr/share/keyrings/pgadmin-archive-keyring.gpg] https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/$(lsb_release -cs) pgadmin4 main" | sudo tee /etc/apt/sources.list.d/pgadmin4.list

2️⃣ 安装 pgAdmin 4

✅ 桌面版(有 GUI,推荐)

sudo apt update
sudo apt install pgadmin4-desktop

✅ Web 版(服务器用)

sudo apt install pgadmin4-web

三、初始化 pgAdmin(Web 版必做)

如果你装的是 pgAdmin Web 版,需要初始化:

sudo /usr/pgadmin4/bin/setup-web.sh

按提示操作:

  • 设置 pgAdmin 登录邮箱
  • 设置 pgAdmin 登录密码

完成后访问:

http://localhost/pgadmin4

四、配置 PostgreSQL 允许 pgAdmin 连接

1️⃣ 修改 PostgreSQL 配置文件

postgresql.conf

sudo nano /etc/postgresql/*/main/postgresql.conf

确认:

listen_addresses = '*'

pg_hba.conf

sudo nano /etc/postgresql/*/main/pg_hba.conf

添加或修改:

# IPv4 local connections
host  all  all  127.0.0.1/32  md5

保存后重启 PostgreSQL:

sudo systemctl restart postgresql

五、在 pgAdmin 中连接数据库

1️⃣ 打开 pgAdmin

  • 桌面版:直接启动
  • Web 版:浏览器访问 http://localhost/pgadmin4

2️⃣ 添加服务器

  1. 右键 Servers → Register → Server
  2. General
    • Name:随便(如 local)
  3. Connection
    • Host:127.0.0.1
    • Port:5432
    • Maintenance DB:postgres
    • Username:postgres
    • Password:PostgreSQL 的密码

如果不知道 postgres 密码:

sudo -u postgres psql
\password postgres

六、常见问题排查

❌ 连接被拒绝

could not connect to server

✅ 检查:

  • PostgreSQL 是否运行
  • listen_addresses
  • 端口是否 5432

❌ 密码错误

password authentication failed

✅ 重置密码:

sudo -u postgres psql -c "ALTER USER postgres PASSWORD 'newpassword';"

❌ Web 版 403 / 404

✅ 确认执行过:

sudo /usr/pgadmin4/bin/setup-web.sh

七、快速总结(最小步骤)

sudo apt install postgresql pgadmin4-desktop
sudo systemctl start postgresql

然后打开 pgAdmin → 连接 127.0.0.1 即可 ✅


如果你告诉我:

  • 桌面版还是服务器版
  • 本地还是远程数据库
  • Ubuntu 版本

我可以给你 定制版配置方案(比如 Docker / Nginx / 远程安全连接)。

0