使用 pgAdmin 远程连接 PostgreSQL 数据库

使用 pgAdmin 远程连接 PostgreSQL 数据库

许久没有用到数据库了。最近做一个物联网应用,需要用到 PostgreSQL 数据库。数据库是安装在 CentOS 7 上的 PostgreSQL 12。平时工作在 Windows 环境下,远程桌面 和 SSH 都不是太方便,最好能在 Windows 下远程操作 PostgreSQL 。

  1. PostgreSQL 默认只监听 localhost,需要修改 postgresql.conf 。

    sudo vi /var/lib/pgsql/12/data/postgresql.conf
    

    找到这一行

    #listen_addresses = 'localhost'
    

    修改为(监听所有地址)

    listen_addresses = '*'
    
  2. PostgreSQL 默认只 允许本地访问,需要修改 pg_hba.conf。

    sudo vi /var/lib/pgsql/12/data/pg_hba.conf
    

    添加一行(记得把 192.168.21.101 替换为你的IP地址)

    host    all             all             192.168.21.101/32       md5
    

    或添加一行(.samenet是允许同网段的所有地址)

    host    all             all             .samenet                md5
    
  3. 重启 PostgreSQL 服务:

    sudo systemctl restart postgresql-12.service
    
  4. 修改 Cent OS 防火墙:

    sudo firewall-cmd --zone=public --add-port=5432/tcp --permanent
    sudo firewall-cmd --reload
    
  5. 安装 pgAdmin。我是直接在 Windows 上安装了个 PostgreSQL 12。这里下载。根据默认提示安装。

  6. 安装后打开 pgAdmin , 点击 Add New Server --> 填写如下选项(其它保持默认值) --> Save。

Field Value
General --> Name Server 的名字或描述,我喜欢直接填 IP 地址
Connection --> Host name/address 填写你的 IP 地址
Connection --> Username PostgreSQL 数据库的用户名
Connection --> Password PostgreSQL 数据库的密码

恭喜你,你应该能看到服务器上的 PostgreSQL 数据库了。

注意:上述操作,并没有考虑任何安全性。

你可能感兴趣的:(数据库,数据库,postgresql,centos,pgAdmin,linux)