一、在centos7上安装PostgreSQL:12

image.png
官网文档地址:[https://www.postgresql.org/download/linux/redhat/](https://www.postgresql.org/download/linux/redhat/)

  1. sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
  2. sudo yum install -y postgresql12-server
  3. sudo /usr/pgsql-12/bin/postgresql-12-setup initdb
  4. sudo systemctl enable postgresql-12
  5. sudo systemctl start postgresql-12

二、navicat远程连接postgres数据库

  • ①:编辑/var/lib/pgsql/12/data/postgresql.conf配置文件:
    1. vim /var/lib/pgsql/12/data/postgresql.conf
    ```bash

    listen_addresses = ‘localhost’

    port = 5432

以上是修改前的配置,如上:

以下是修改后的配置,如下:

listen_addresses = ‘*’ port = 5432

  1. - ②:编辑`/var/lib/pgsql/12/data/pg_hba.conf`配置文件:
  2. > 使用`navicat`连接`posgres`数据库时,需要将`METHOD`类型配置为`md5`。具体参考下边的案例。
  3. ```bash
  4. vim /var/lib/pgsql/12/data/pg_hba.conf
  1. # TYPE DATABASE USER ADDRESS METHOD
  2. # "local" is for Unix domain socket connections only
  3. local all all peer
  4. # IPv4 local connections:
  5. host all all 127.0.0.1/32 ident
  6. host all all 0.0.0.0/0 ident
  7. # IPv6 local connections:
  8. host all all ::1/128 ident
  9. # Allow replication connections from localhost, by a user with the
  10. # replication privilege.
  11. local replication all peer
  12. host replication all 127.0.0.1/32 ident
  13. host replication all ::1/128 ident
  14. 以上是修改前的配置,如上:
  15. =========================================================================
  16. 以下是修改后的配置,如下:
  17. # TYPE DATABASE USER ADDRESS METHOD
  18. # "local" is for Unix domain socket connections only
  19. local all all peer
  20. # IPv4 local connections:
  21. host all all 127.0.0.1/32 md5
  22. host all all 0.0.0.0/0 md5 # 新增一行
  23. # IPv6 local connections:
  24. host all all ::1/128 md5
  25. # Allow replication connections from localhost, by a user with the
  26. # replication privilege.
  27. local replication all peer
  28. host replication all 127.0.0.1/32 ident
  29. host replication all ::1/128 ident
  • ③:重启PostgreSQL服务
    1. sudo systemctl restart postgresql-12

三、修改PostgreSQL数据库默认用户postgres的密码

PostgreSQL数据库创建一个postgres用户作为数据库的管理员,密码随机,所以需要修改密码,方式如下:

  • ①:登录PostgreSQL

    1. sudo -u postgres psql
  • ②:修改登录PostgreSQL密码

    1. ALTER USER postgres WITH PASSWORD 'postgres';
    • 密码postgres要用引号引起来。
    • 命令最后有;分号。
  • ③:退出PostgreSQL客户端

    1. \q
  • ④:重启PostgreSQL服务

    1. sudo systemctl restart postgresql-12

四、防火墙开发PostgreSQL数据库的端口号

  • ①:开发指定端口号

    1. sudo firewall-cmd --zone=public --add-port=5432/tcp --permanent
  • ②:更新防火墙规则

    1. sudo firewall-cmd --reload
  • ③:查看所有打开的端口

    1. sudo firewall-cmd --zone=public --list-ports