温馨提示×

linux pgadmin怎么连接

小樊
33
2026-08-05 07:36:44
栏目: 智能运维

在 Linux 上用 pgAdmin 连接 PostgreSQL,一般分 pgAdmin 安装 → 数据库准备 → 建立连接 三步。下面按最常见场景说明(pgAdmin 4 + PostgreSQL)。


一、确认 PostgreSQL 已启动

sudo systemctl status postgresql

如果没启动:

sudo systemctl start postgresql

二、准备 PostgreSQL 允许连接(很重要)

1️⃣ 修改 pg_hba.conf

路径一般在:

/var/lib/pgsql/15/data/pg_hba.conf
# 或
/etc/postgresql/15/main/pg_hba.conf

确保有类似这一行(允许本地连接):

local   all   all   md5
# 或
host    all   all   127.0.0.1/32   md5

修改后重启:

sudo systemctl restart postgresql

2️⃣ 设置数据库用户密码

sudo -u postgres psql
ALTER USER postgres PASSWORD 'your_password';
\q

三、安装 pgAdmin 4(任意一种)

✅ 方式一:APT(Ubuntu / Debian)

sudo apt update
sudo apt install pgadmin4

浏览器访问:

http://127.0.0.1/pgadmin4

✅ 方式二:Python 虚拟环境(通用 Linux)

python3 -m venv pgadmin
source pgadmin/bin/activate
pip install pgadmin4
pgadmin4

四、pgAdmin 连接 PostgreSQL

1️⃣ 打开 pgAdmin

浏览器访问:

http://127.0.0.1:5050

(端口可能不同)


2️⃣ 添加服务器

  1. 左侧 Servers → 右键 → Register → Server
  2. General 页
    • Name:随便(如 local-db
  3. Connection 页
    • Host name/address:localhost127.0.0.1
    • Port:5432
    • Maintenance database:postgres
    • Username:postgres
    • Password:刚才设置的密码
  4. 勾选 Save password
  5. 点击 Save

五、常见连接失败原因

问题 解决方法
password authentication failed 用户名/密码错误
could not connect PostgreSQL 没启动
timeout 防火墙 / 端口不对
FATAL: role does not exist 用户不存在

六、远程连接(如果你需要)

  1. postgresql.conf
listen_addresses = '*'
  1. pg_hba.conf
host all all 0.0.0.0/0 md5
  1. 开放端口:
sudo ufw allow 5432

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

  • ✅ Linux 发行版(Ubuntu / CentOS / Arch)
  • ✅ PostgreSQL 版本
  • ✅ 本地还是远程连接

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

0