温馨提示×

Linux下PgAdmin怎么配置

小樊
48
2026-07-21 16:13:45
栏目: 智能运维

在 Linux 下配置 pgAdmin(主要用于管理 PostgreSQL 数据库),常见有两种方式:pgAdmin 4(Web 模式,推荐)pgAdmin 4 Desktop 模式。下面按最常见、最稳妥的方式一步一步说明。


一、安装 pgAdmin 4(Linux 通用方式)

✅ 方式一:使用官方仓库(推荐,适用于 Debian / Ubuntu / RHEL / CentOS)

1️⃣ 安装依赖

sudo apt update
sudo apt install curl wget gnupg2

2️⃣ 添加 pgAdmin 仓库(Ubuntu / Debian 示例)

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️⃣ 安装 pgAdmin 4

  • Web 模式(服务器常用)
sudo apt update
sudo apt install pgadmin4-web
  • 桌面模式(个人开发)
sudo apt install pgadmin4-desktop

二、配置 pgAdmin 4(Web 模式重点)

✅ 1️⃣ 初始化 pgAdmin

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

会提示你:

  • 设置 pgAdmin 管理员邮箱
  • 设置 密码

✅ 记住这个账号,这是 pgAdmin 的登录账号,不是 PostgreSQL 的账号。


✅ 2️⃣ 启动 Apache(如果使用 Web 模式)

sudo systemctl restart apache2

访问地址:

http://localhost/pgadmin4

或:

http://服务器IP/pgadmin4

三、连接 PostgreSQL 数据库

✅ 1️⃣ 登录 pgAdmin

使用刚才设置的 邮箱 + 密码


✅ 2️⃣ 添加 PostgreSQL 服务器

  1. 左侧 Servers → 右键 → Register → Server
  2. General 页
    • Name:任意(如 local-db
  3. Connection 页
    • Host name/address:127.0.0.1 或 PostgreSQL 服务器 IP
    • Port:5432
    • Maintenance database:postgres
    • Username:postgres
    • Password:PostgreSQL 用户密码
  4. ✅ 勾选 Save password

四、常见问题与解决

❌ 1. 无法连接 PostgreSQL

检查 PostgreSQL 是否允许远程连接

sudo nano /etc/postgresql/版本/main/pg_hba.conf

确保有:

host  all  all  0.0.0.0/0  md5

修改监听地址

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

重启 PostgreSQL:

sudo systemctl restart postgresql

❌ 2. pgAdmin 页面打不开

sudo systemctl status apache2
sudo journalctl -xe

❌ 3. PostgreSQL 用户不存在

sudo -u postgres psql
CREATE USER youruser WITH PASSWORD 'yourpassword';
GRANT ALL PRIVILEGES ON DATABASE yourdb TO youruser;

五、桌面模式(适合个人开发)

如果你只是本地开发:

sudo apt install pgadmin4-desktop
pgadmin4

直接打开 GUI 界面,无需 Apache。


六、推荐配置总结

场景 推荐方式
服务器 pgAdmin 4 Web
本地开发 pgAdmin 4 Desktop
生产环境 pgAdmin + Nginx + HTTPS

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

  • ✅ Linux 发行版(Ubuntu / CentOS / Rocky / Debian)
  • ✅ PostgreSQL 是否已安装
  • ✅ 本地还是远程数据库

我可以给你完全针对你环境的配置命令

0