温馨提示×

ubuntu pgadmin如何添加主机

小樊
46
2026-10-12 13:37:29
栏目: 云计算

在 Ubuntu 上使用 pgAdmin 添加 PostgreSQL 主机(服务器)的步骤如下:


一、前提条件

  1. 已安装 pgAdmin(桌面版或 Web 版)
  2. PostgreSQL 服务已启动
  3. 知道以下信息:
    • 主机地址(如 localhost 或远程 IP)
    • 端口(默认 5432)
    • 用户名(如 postgres)
    • 密码
    • 数据库名(可选)

二、pgAdmin 添加主机步骤

方法一:pgAdmin 桌面版(pgAdmin 4)

  1. 打开 pgAdmin 4
  2. 左侧 Browser 面板中,右键 Servers
  3. 选择 Register → Server
  4. 在弹出窗口中填写:
    • General 选项卡
      • Name:自定义名称(如 MyPG)
    • Connection 选项卡
      • Host name/address:localhost 或服务器 IP
      • Port:5432
      • Maintenance database:postgres
      • Username:postgres
      • Password:数据库密码
      • 勾选 Save password
  5. 点击 Save

方法二:pgAdmin Web 版(Ubuntu 服务器安装)

  1. 浏览器访问:
    http://localhost/pgadmin4
    
  2. 登录 pgAdmin
  3. 左侧右键 Servers → Register → Server
  4. 填写同上(Host、Port、Username、Password)
  5. 保存

三、常见问题

1. 无法连接(Connection refused)

  • PostgreSQL 未启动:

    sudo systemctl start postgresql
    
  • 检查监听地址:

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

    确保:

    listen_addresses = 'localhost'
    

    或远程访问改为 '*'

  • 修改 pg_hba.conf 允许连接:

    host all all 0.0.0.0/0 md5
    

    然后重启:

    sudo systemctl restart postgresql
    

2. 远程连接失败

  • 云服务器需开放 5432 端口
  • 防火墙:
    sudo ufw allow 5432
    

如果你是用 桌面 pgAdmin 还是 Web 版,或者连接的是 本地 / 远程服务器,可以告诉我,我可以给你更具体的配置示例。

0 踩