温馨提示×

如何连接Linux的PgAdmin

小樊
43
2026-07-21 16:23:43
栏目: 智能运维

要在 Linux 上连接并使用 PgAdmin 管理 PostgreSQL,一般分为 安装 PgAdmin配置 PostgreSQL建立连接 三个步骤。下面按常见场景详细说明。


一、安装 PgAdmin(Linux)

✅ 方式一:安装 PgAdmin 4(推荐,Web 版)

1. 添加 PostgreSQL 官方仓库(以 Ubuntu 为例)

sudo apt update
sudo apt install curl ca-certificates
curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo gpg --dearmor -o /usr/share/keyrings/postgresql-keyring.gpg

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

2. 安装 PgAdmin 4

sudo apt update
sudo apt install pgadmin4

3. 启动 PgAdmin Web 服务

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

按提示设置 PgAdmin 登录邮箱和密码

浏览器访问:

http://localhost/pgadmin4

✅ 方式二:使用 Python 虚拟环境安装(适合服务器)

sudo apt install python3-venv
python3 -m venv pgadmin4
source pgadmin4/bin/activate
pip install pgadmin4
pgadmin4

二、配置 PostgreSQL 允许连接

1. 修改 pg_hba.conf

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

添加或修改:

# 本地连接
local   all   all                md5

# 远程连接(如需要)
host    all   all   0.0.0.0/0     md5

2. 修改 postgresql.conf

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

确保:

listen_addresses = '*'

3. 重启 PostgreSQL

sudo systemctl restart postgresql

三、在 PgAdmin 中连接 PostgreSQL

1. 打开 PgAdmin

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

2. 添加服务器

  1. 右键 Servers → Register → Server
  2. 填写:

General 页

  • Name:自定义(如 local-postgres

Connection 页

  • Host name/address
    • 本机:localhost127.0.0.1
    • 远程:服务器 IP
  • Port5432
  • Maintenance databasepostgres
  • Usernamepostgres
  • Password:PostgreSQL 用户密码
  • ✅ 勾选 Save password

3. 点击 Save


四、常见问题排查

❌ 连接失败:could not connect to server

✅ 检查:

sudo systemctl status postgresql

❌ 远程连接失败

✅ 检查:

  • 防火墙是否放行 5432
sudo ufw allow 5432

❌ 密码错误

sudo -u postgres psql
\password postgres

五、总结流程

安装 PgAdmin
↓
配置 PostgreSQL(pg_hba.conf / postgresql.conf)
↓
启动 PostgreSQL
↓
PgAdmin 添加 Server
↓
连接成功

如果你告诉我:

  • Ubuntu / CentOS / Debian
  • 本地还是远程连接
  • 是否需要远程访问

我可以给你 精确到命令级别 的教程。

0