暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

连接到PostgreSQL数据库

oracleace 2023-08-29
340

关于号主,姚远:

  • Oracle ACE(Oracle和MySQL数据库方向)

  • 华为云最有价值专家

  • 《MySQL 8.0运维与优化》的作者

  • 拥有 Oracle 10g、12c和19c OCM等数十项数据库认证

  • 曾任IBM公司数据库部门经理

  • 20+年DBA经验,服务2万+客户

  • 精通C和Java,发明两项计算机专利


01

PG的默认链接规则


PG默认只接受本地的访问,这个规则是由参数listen_addresses控制的

    postgres=> show listen_addresses;
    listen_addresses
    ------------------
    localhost
    (1 row)

    可以把修改这个参数用于指定访问的ip,也可以用星号允许所有的IP访问。

      listen_addresses = 'localhost,192.168.87.178'

      pg_hba.conf 文件其全名为 "PostgreSQL Host-Based Authentication Configuration"。该文件用于管理数据库系统中客户端连接的身份验证和访问控制。它决定了哪些主机和用户可以连接到数据库以及以何种方式连接。默认只允许本地连接,且连接认证方式均为 trust,生产环境建议更改为 md5 连接认证方式,并根据需求增加允许访问的客户端地址。


      02

      本地操作系统用户无密码连接


      修改pg_hba.conf 文件

        local   all             all          ident map=map_name

        这里的ident也可以改成peer,因为当为一个本地(非 TCP/IP)连接指定 ident 时,将实际使用 peer 认证。

        修改pg_hba.conf 文件

          # MAPNAME       SYSTEM-USERNAME         PG-USERNAME
          map_name root postgres
          map_name postgres postgres

          下面的命令载入修改后的配置

            -bash-4.2$ usr/pgsql-14/bin/pg_ctl reload
            server signaled

            或者

              postgres=> select pg_reload_conf();

              使用root用户登录测试

                [root@pg data]# id
                uid=0(root) gid=0(root) groups=0(root)
                [root@pg data]# psql -U postgres
                psql (14.3)
                Type "help" for help.


                postgres=# select user;
                user
                ----------
                postgres
                (1 row)


                03

                远程TCP/IP登录


                pg_hba.conf 文件中对应的配置如下:

                  host    all             all             0.0.0.0/0            md5

                  远程登录:

                    root@YaoYuan ~# psql -U postgres -h 192.168.18.198
                    Password for user postgres:
                    psql (15.3, server 14.3)
                    Type "help" for help.


                    postgres=# select user;
                    user
                    ----------
                    postgres
                    (1 row)


                    04

                    ident认证


                    首先要安装服务,不然会出现下面的错误:

                      2023-08-08 07:38:07.213 UTC [25746] LOG:  could not connect to Ident server at address "192.168.87.178", port 113: Connection refused

                      安装方法如下:

                        --linux 客户端安装 oidentd,并启动该服务
                        yum -y install epel-release
                        yum clean all && yum makecache
                        yum install -y oidentd

                        pg_hba.conf 文件中对应的配置如下:

                          host    all             all             0.0.0.0/0            ident map=map_name

                          使用root用户登录测试:

                            [root@pg data]# psql -U postgres -h 192.168.18.198
                            psql (14.3)
                            Type "help" for help.


                            postgres=# select user;
                            user
                            ----------
                            postgres
                            (1 row)

                            可以不用密码登录


                            欢迎加我的微信,拉你进数据库微信群👇

                            推荐文章👇

                            试看《MySQL 8.0运维与优化》(清华大学出版社)

                            托业890分的Oracle ACE为您翻译国际大佬的雄文(合集)


                            文章转载自oracleace,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

                            评论