温馨提示×

ubuntu上pgadmin怎么用

小樊
42
2026-09-27 22:21:31
栏目: 智能运维

在 Ubuntu 上使用 pgAdmin(PostgreSQL 的图形化管理工具)一般分为 安装 → 启动 → 连接数据库 → 基本使用 几个步骤。下面按常见场景给你说明。


一、安装 pgAdmin(Ubuntu)

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

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

# 2. 导入 pgAdmin 官方仓库
curl -fsS https://www.pgadmin.org/static/packages_pgadmin_org.pub | sudo gpg --dearmor -o /usr/share/keyrings/packages-pgadmin-org.gpg

# 3. 添加软件源
sudo sh -c 'echo "deb [signed-by=/usr/share/keyrings/packages-pgadmin-org.gpg] https://ftp.pgadmin.org/pub/pgadmin/pgadmin4/apt/ $(lsb_release -cs) pgadmin4" > /etc/apt/sources.list.d/pgadmin4.list'

# 4. 安装
sudo apt update
sudo apt install pgadmin4

安装完成后:

  • 桌面版:直接搜索 pgAdmin 4
  • Web 版:访问 http://localhost/pgadmin4

方式二:用 pip 安装(轻量,Web 模式)

pip install pgadmin4

适合开发环境,不推荐生产。


二、启动 pgAdmin

桌面版

pgadmin4

或在菜单中点击 pgAdmin 4

Web 版(Apache 模式)

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

然后浏览器打开:

http://localhost/pgadmin4

三、第一次使用(配置 master 密码)

首次启动会要求设置 Master Password,用于加密本地连接配置。


四、连接 PostgreSQL 数据库

  1. 打开 pgAdmin
  2. 左侧 Servers → 右键 → Register → Server
  3. 填写:
    • General
      • Name:随意(如 local)
    • Connection
      • Host: 127.0.0.1 或 localhost
      • Port: 5432
      • Maintenance DB: postgres
      • Username: postgres
      • Password: 安装 PG 时设置的密码
  4. 点击 Save

✅ 连接成功后可看到数据库、表、用户等


五、常见操作

1️⃣ 创建数据库

Databases → 右键 → Create → Database

2️⃣ 执行 SQL

选数据库 → Tools → Query Tool

3️⃣ 查看表数据

表 → 右键 → View/Edit Data

4️⃣ 导入 / 导出

  • 表 → 右键 → Import/Export
  • 支持 CSV

六、常见问题

❌ 连不上 PostgreSQL

检查:

sudo systemctl status postgresql
# /etc/postgresql/*/main/postgresql.conf
listen_addresses = 'localhost'

# pg_hba.conf
host all all 127.0.0.1/32 md5

❌ 权限错误

sudo -u postgres psql
ALTER USER postgres PASSWORD '新密码';

七、使用建议

  • 本地开发:用 桌面版
  • 服务器管理:用 Web 版(小心安全)
  • 生产环境:建议配合 SSH + 内网访问

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

  • Ubuntu 版本
  • PostgreSQL 是否已安装
  • 用桌面还是服务器

我可以给你更精确的一步一步操作。

0 踩