温馨提示×

Ubuntu下PgAdmin怎么配置

小樊
42
2026-08-06 03:12:11
栏目: 智能运维

下面分 Ubuntu 系统环境PgAdmin 版本 给你一个完整、实用的配置说明。目前主流是 PgAdmin 4(Web 版),我重点讲这个,最后也会简单提旧版 PgAdmin 3。


一、安装 PgAdmin 4(Ubuntu)

✅ 推荐方式:通过官方仓库安装(最稳定)

sudo apt update
sudo apt install pgadmin4

安装完成后,系统会自动配置好 Web 服务。


二、初始化 PgAdmin 4(首次使用必须)

1️⃣ 设置 PgAdmin 管理员账号

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

会提示你输入:

  • Email(作为登录账号)
  • Password(PgAdmin 登录密码)

示例:

Email: admin@example.com
Password: ********

2️⃣ 启动服务

sudo systemctl restart apache2

PgAdmin 4 是通过 Apache + WSGI 运行的。


三、访问 PgAdmin 4

浏览器访问:

http://localhost/pgadmin4

或服务器 IP:

http://<服务器IP>/pgadmin4

使用刚才设置的 Email + Password 登录。


四、配置连接 PostgreSQL 数据库

1️⃣ 登录后点击「Add New Server」

2️⃣ General 标签页

  • Name:随便起(如 local-db

3️⃣ Connection 标签页(重点)

参数 说明
Host name/address localhost 或数据库 IP
Port 5432
Maintenance database postgres
Username PostgreSQL 用户名
Password 用户密码
Save password ✅ 勾选

点击 Save 即可。


五、常见问题与配置

✅ 1. 连接失败:peer authentication

错误示例:

FATAL: Peer authentication failed for user "postgres"

解决方法(推荐)

编辑 PostgreSQL 配置文件:

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

找到:

local   all   all   peer

改为:

local   all   all   md5

然后重启:

sudo systemctl restart postgresql

✅ 2. 远程连接 PostgreSQL

PostgreSQL 配置

1️⃣ 修改 postgresql.conf

sudo nano /etc/postgresql/15/main/postgresql.conf
listen_addresses = '*'

2️⃣ 修改 pg_hba.conf 追加:

host  all  all  0.0.0.0/0  md5

3️⃣ 重启 PostgreSQL

sudo systemctl restart postgresql

✅ 3. 如果 PgAdmin 页面打不开

检查 Apache 状态:

sudo systemctl status apache2

检查端口:

sudo netstat -tlnp | grep 80

六、PgAdmin 3(不推荐)

Ubuntu 20.04 以后 不再官方支持 PgAdmin 3,建议直接用 PgAdmin 4。


七、总结流程(速查)

# 安装
sudo apt install pgadmin4

# 初始化
sudo /usr/pgadmin4/bin/setup-web.sh

# 重启服务
sudo systemctl restart apache2

# 访问
http://localhost/pgadmin4

如果你愿意,可以告诉我:

  • Ubuntu 版本(如 20.04 / 22.04)
  • PostgreSQL 是否已安装
  • 是本地还是服务器环境

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

0