温馨提示×

ubuntu pgadmin怎样连接

小樊
44
2026-10-24 07:32:46
栏目: 智能运维

在 Ubuntu 上使用 pgAdmin 连接 PostgreSQL 数据库,一般分两种情况:连接本地 PostgreSQL 和 连接远程 PostgreSQL。下面按常见步骤说明。


一、在 Ubuntu 上安装 pgAdmin

方式 1:pgAdmin 4(Web 版,推荐)

sudo apt update
sudo apt install pgadmin4 pgadmin4-web

安装完成后初始化:

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

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

启动 Apache:

sudo systemctl restart apache2

浏览器访问:

http://localhost/pgadmin4

方式 2:桌面版(如果有 GUI)

sudo apt install pgadmin4-desktop

直接从应用菜单打开 pgAdmin。


二、连接本地 PostgreSQL

1. 确保 PostgreSQL 已安装并运行

sudo systemctl status postgresql

若未启动:

sudo systemctl start postgresql

2. pgAdmin 中添加连接

  1. 打开 pgAdmin
  2. 右侧 Servers → 右键 → Create → Server
  3. 在 General 页:
    • Name:随便写(如 localdb)
  4. 在 Connection 页:
    • Host: localhost
    • Port: 5432
    • Maintenance database: postgres
    • Username: postgres
    • Password:安装 PostgreSQL 时设置的密码

⚠️ 如果不知道 postgres 密码:

sudo -u postgres psql
\password postgres

三、连接远程 PostgreSQL(常见坑)

1. 远程服务器配置

在远程服务器上修改:

postgresql.conf

listen_addresses = '*'

pg_hba.conf

host    all    all    0.0.0.0/0    md5

重启 PostgreSQL:

sudo systemctl restart postgresql

2. 防火墙

sudo ufw allow 5432

3. pgAdmin 连接

  • Host:远程服务器 IP
  • Port:5432
  • Username / Password:远程数据库账号

四、常见连接失败原因

  • ❌ connection refused → PostgreSQL 没启动或只监听 localhost
  • ❌ password authentication failed → 用户名或密码错误
  • ❌ 超时 → 防火墙 / 安全组未放行 5432

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

  • 是 本地还是远程
  • pgAdmin 是 Web 还是桌面
  • 报错信息是什么

我可以一步步帮你排错。

0 踩