centos7 gnome postgresql-11 + postgis31_11 + pgadmin4-deskto

本文详细介绍了在 CentOS 7 上从官网下载并安装 PostgreSQL 11,配置监听地址、远程访问权限,安装 PostGIS 3.1.0,以及迁移数据和调整防火墙设置的过程,适合数据库管理员参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


安装 PostgreSQL 11 官网安装链接

  1. yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm 装了这个 rpm ,才能 yum search
  2. yum install -y postgresql11
  3. yum install -y postgresql11-server
  4. /usr/pgsql-11/bin/postgresql-11-setup initdb 直接执行
  5. psql --version 查看 postgres 版本
  6. systemctl enable postgresql-11 设置开机启动
  7. systemctl start postgresql-11 启动
  8. systemctl stop postgresql-11 关闭,或 lsof -i:5432 查询 pid ,kill pid
  9. 修改 postgres 用户密码 配置方法
    1. 先切换到 root
    2. passwd postgres ,修改 postgres 用户密码
  10. 修改 postgres 数据库管理员密码
    1. 先启动数据库 systemctl start postgresql-11
    2. su - postgres 切换用户,执行后提示符会变为 ‘-bash-4.2$’,切换为UNIX风格的bash
    3. psql -U postgres 以 postgres 的身份登录数据库,执行后提示符变为 ‘postgres=#’ ( windows 使用 psql 需要将 psql.exe 所在目录加入环境变量,加到 Path 后即可,重启后生效)
    4. ALTER USER postgres WITH PASSWORD 'abc123'; 设置postgres用户密码,\q 退出,此时可切回 root su - root
  11. 开启远程访问(注意修改.conf文件后要重启数据库 systemctl restart postgresql-11
    1. 修改 /var/lib/pgsql/11/data/postgresql.conf(端口号也在其中配置) 中的 #listen_addresses = ‘localhost’ 为 listen_addresses=’*’ 当然,此处‘*’也可以改为任何你想开放的服务器IP
    2. 修改文件可以用 xftp 拖出来修改然后替换回去(注意备份),不要先删除再把新文件拖进去,这样会改变属性和所有者,之后要 root 下 chmod 修改属性,chrown 修改所有者
  12. 信任远程连接(注意修改.conf文件后要重启数据库 systemctl restart postgresql-11
    1. 信任指定服务器连接
      修改 /var/lib/pgsql/11/data/pg_hba.conf 中的
      # IPv4 local connections: 
      host all all 127.0.0.1/32 trust  # 信任本机应用的连接,如 dbeaver ident 连接如果报错,要改成 trust, password (密码明文), md5 (加密的密码)
      host all all 192.168.230.1/32(需要连接的服务器IP) md5
      
      先不改动也行,等后面用 DBeaver 或 navicat 工具连接时( sqlalchemy 程序连接也会)数据库会报错 “致命错误:没有用于主机"192.168.230.1",用户"postgres"” 的提示,再把 host all all 192.168.230.1/32 trust 加上
      方便起见填写 host all all 192.168.0.0/16 md5 即可
      或加 host all all 0.0.0.0/0 md5
  13. 开启 5432 端口防火墙
    firewall-cmd --zone=public --add-port=5432/tcp --permanent
    firewall-cmd --reload
  14. 日常开启关闭
    开启 systemctl start postgresql-11
    重启 systemctl restart postgresql-11
    关闭 systemctl stop postgresql-11

CentOS7 安装 PostGIS 3.1.0

  1. 先安装 PostgreSQL 11

  2. yum install epel-release.noarch

  3. yum install postgis31_11

  4. rpm -qi postgis31_11

    查看版本信息
    Name : postgis31_11
    Version : 3.1.0
    Release : 1.rhel7
    Architecture: x86_64
    Install Date: 2021年01月17日 星期日 09时34分32秒
    Group : Unspecified
    Size : 34608618
    License : GPLv2+
    Signature : DSA/SHA1, 2020年12月23日 星期三 00时37分33秒, Key ID 1f16d2e1442df0f8
    Source RPM : postgis31_11-3.1.0-1.rhel7.src.rpm
    Build Date : 2020年12月23日 星期三 00时37分24秒
    Build Host : koji-centos7-x86-64-pgbuild
    Relocations : (not relocatable)
    Vendor : PostgreSQL Global Development Group
    URL : http://www.postgis.net/
    Summary : Geographic Information Systems Extensions to PostgreSQL
    Description :
    PostGIS adds support for geographic objects to the PostgreSQL object-relational
    database. In effect, PostGIS “spatially enables” the PostgreSQL server,
    allowing it to be used as a backend spatial database for geographic information
    systems (GIS), much like ESRI’s SDE or Oracle’s Spatial extension. PostGIS
    follows the OpenGIS “Simple Features Specification for SQL” and has been
    certified as compliant with the “Types and Functions” profile.

  5. 创建一个叫 history_data 的数据库,并启用 PostGIS Spatial 功能

    1. su postgres
    2. psql -U postgres
    3. 如果先前修改过端口号psql -p 33245 -U postgres
    4. 若嫌烦则
      1. 进入 /home 创建 postgres 文件夹(若无)
      2. 新建 .bash_profile 文件,输入 export PGPORT=33245 ( GNOME 下 ctrl + h 显示隐藏文件)
      3. su postgres
      4. 在 /home/postgres 中 source .bash_profile
      5. su root
      6. systemctl restart postgresql-11
      7. su postgres 即可直接 psql -U postgres 进入
    5. create database "history_data"
    6. \c "hisotory_data"
    7. create extension postgis;
    8. 验证安装 select PostGIS_version(); 返回 3.1 USE_GEOS=1 USE_PROJ=1 USE_STATS=1
    9. 验证安装 select count(*) from spatial_ref_sys; 返回 8500
    10. \q 退出 postgres 数据库用户
    11. psql -U postgres 后,连接数据库之前 select * from pg_database; 查看全部数据库
    12. 连接数据库之前 drop database "history_data"; 删除数据库
    13. su root 切回 root 用户

postgresql-11 数据搬迁至有更大存储挂载的 /hiit 下

  1. systemctl stop postgresql-11
  2. cp -r /var/lib/pgsql /hiit
  3. rm -rf /hiit/pgsql/.bash_history /hiit/pgsql/.bash_profile /hiit/pgsql/.psql_history /hiit/pgsql/11/backups /hiit/pgsql/11/initdb.log
  4. chown -R postgres.postgres /hiit/pgsql
  5. 修改 /usr/lib/systemd/system/postgresql-11.service
    # Location of database directory
    Environment=PGDATA=/var/lib/pgsql/11/data/
    
    改为
    # Location of database directory
    Environment=PGDATA=/hvit/pgsql/11/data/
    
  6. systemctl daemon-reload
  7. systemctl restart postgresql-11

rpm 安装桌面版 pgadmin 4

  1. rpm -i https://ftp.postgresql.org/pub/pgadmin/pgadmin4/yum/pgadmin4-redhat-repo-2-1.noarch.rpm
  2. yum install -y pgadmin4-desktop

[旧] CentOS7 安装 PostGIS 2.5 教程1教程2

  1. 先安装 PostgreSQL 11
  2. 为 PostgreSQL 11 安装 PostGIS v2.5 yum install postgis25_11 (pg12对应 postgis30_11 ),若 gdal 报错则先 yum install epel-release.noarch
  3. 验证安装,检查安装的版本 rpm -qi postgis25_11 会返回信息表
  4. 创建一个叫 test_postgis 的数据库,并启用 PostGIS Spatial 功能
    1. su - postgres
    2. psql -U postgres
    3. create database test_postgis;
    4. \c test_postgis 连接数据库
    5. create extension postgis;
    6. 验证安装 select PostGIS_version(); 返回 2.5 USE_GEOS=1 USE_PROJ=1 USE_STATS=1
    7. 验证安装 select count(*) from spatial_ref_sys; 返回 5757
    8. \q 退出psql
    9. su root 切回 root 用户
    10. 连接数据库之前 select * from pg_database;psql -l 查看全部数据库
    11. 连接数据库之前 drop database testbase; 删除数据库
    12. 数据库更名 alter database "wenzhou_qhdm" rename to "wenzhou_QHDM";

防火墙

  • 查看防火墙规则
    firewall-cmd --list-all

  • 查看端口是否开启
    firewall-cmd --zone=public --query-port=8080/tcp

  • 重启防火墙
    firewall-cmd --reload

  • 开启端口(不加 --permanent ,在 firewall-cmd --reload 后失效)
    firewall-cmd --zone=public --add-port=5432/tcp

  • 永久开启端口
    firewall-cmd --zone=public --add-port=5432/tcp --permanent
    firewall-cmd --reload

  • 关闭端口
    firewall-cmd --zone=public --remove-port=8083/tcp [--permanent]
    firewall-cmd --reload

  • FirewallD is not running
    systemctl status firewalld 查看 firewalld 状态
    若 inactive (dead) 执行 systemctl start firewalld 开启
    systemctl stop firewalld 关闭


端口占用

  1. 查看端口(使用 yum install lsof 安装 lsofsudo lsof -i:8083
  2. 结束进程 kill (PID)
  3. 强制结束进程 kill -s 9 (PID)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值