温馨提示×

ubuntu pgadmin怎样配置

小樊
45
2026-09-27 22:22:32
栏目: 智能运维

在 Ubuntu 上配置 pgAdmin(以 pgAdmin 4 为例,目前最常用)一般分为 安装 和 连接 PostgreSQL 两部分。下面给你一套从零开始的清晰步骤。


一、安装 pgAdmin 4(Ubuntu)

方式一:通过官方 APT 仓库安装(推荐)

# 1. 安装依赖
sudo apt update
sudo apt install -y curl ca-certificates gnupg

# 2. 添加 pgAdmin 官方仓库
curl -fsSL https://www.pgadmin.org/static/packages_pgadmin_org.pub | \
  sudo gpg --dearmor -o /usr/share/keyrings/pgadmin.gpg

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

# 3. 安装
sudo apt update
sudo apt install -y pgadmin4

如果是 桌面版 Ubuntu,推荐:

sudo apt install -y pgadmin4-desktop

如果是 服务器(无界面),用:

sudo apt install -y pgadmin4-web

二、配置 pgAdmin 4 Web 模式(服务器常用)

如果你安装的是 pgadmin4-web:

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

按提示设置:

  • 管理员邮箱
  • 管理员密码
  • 是否配置 Apache

配置完成后访问:

http://服务器IP/pgadmin4

三、配置 PostgreSQL 允许连接

1. 修改 PostgreSQL 配置

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

确保:

listen_addresses = 'localhost'   # 或 '0.0.0.0'

2. 修改客户端认证

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

示例(本地连接):

host    all    all    127.0.0.1/32    md5

若允许远程:

host    all    all    0.0.0.0/0    md5

⚠️ 生产环境不建议 0.0.0.0/0

重启 PostgreSQL:

sudo systemctl restart postgresql

四、在 pgAdmin 中添加数据库服务器

  1. 打开 pgAdmin
  2. 右侧 Servers → 右键 → Register → Server
  3. 填写:
    • General
      • Name: 自定义(如 LocalPG)
    • Connection
      • Host: 127.0.0.1 或服务器 IP
      • Port: 5432
      • Maintenance database: postgres
      • Username: postgres
      • Password: 安装 PG 时设置的密码
  4. 点击 Save

五、常见问题

❌ 无法连接

  • PostgreSQL 没启动
sudo systemctl status postgresql
  • 防火墙未放行
sudo ufw allow 5432

❌ pgAdmin 打不开网页

  • 检查 Apache / Nginx
sudo systemctl status apache2

六、总结

✅ Ubuntu 上配置 pgAdmin 核心三步:

  1. 安装 pgAdmin 4
  2. 配置 PostgreSQL 允许连接
  3. pgAdmin 中添加 Server

如果你告诉我:

  • 是 桌面版还是服务器版 Ubuntu
  • 是否 远程连接 PostgreSQL

我可以给你更精确的配置方案。

0 踩