安装 PostgreSQL 11 官网安装链接
yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
装了这个 rpm ,才能 yum searchyum install -y postgresql11
yum install -y postgresql11-server
/usr/pgsql-11/bin/postgresql-11-setup initdb
直接执行psql --version
查看 postgres 版本systemctl enable postgresql-11
设置开机启动systemctl start postgresql-11
启动systemctl stop postgresql-11
关闭,或lsof -i:5432
查询 pid ,kill pid- 修改 postgres 用户密码 配置方法
- 先切换到 root
passwd postgres
,修改 postgres 用户密码
- 修改 postgres 数据库管理员密码
- 先启动数据库
systemctl start postgresql-11
su - postgres
切换用户,执行后提示符会变为 ‘-bash-4.2$’,切换为UNIX风格的bashpsql -U postgres
以 postgres 的身份登录数据库,执行后提示符变为 ‘postgres=#’ ( windows 使用 psql 需要将 psql.exe 所在目录加入环境变量,加到 Path 后即可,重启后生效)ALTER USER postgres WITH PASSWORD 'abc123';
设置postgres用户密码,\q
退出,此时可切回 rootsu - root
- 先启动数据库
- 开启远程访问(注意修改.conf文件后要重启数据库
systemctl restart postgresql-11
)- 修改 /var/lib/pgsql/11/data/postgresql.conf(端口号也在其中配置) 中的 #listen_addresses = ‘localhost’ 为 listen_addresses=’*’ 当然,此处‘*’也可以改为任何你想开放的服务器IP
- 修改文件可以用 xftp 拖出来修改然后替换回去(注意备份),不要先删除再把新文件拖进去,这样会改变属性和所有者,之后要 root 下
chmod
修改属性,chrown
修改所有者
- 信任远程连接(注意修改.conf文件后要重启数据库
systemctl restart postgresql-11
)- 信任指定服务器连接
修改 /var/lib/pgsql/11/data/pg_hba.conf 中的
先不改动也行,等后面用 DBeaver 或 navicat 工具连接时( sqlalchemy 程序连接也会)数据库会报错 “致命错误:没有用于主机"192.168.230.1",用户"postgres"” 的提示,再把# 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
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
- 信任指定服务器连接
- 开启 5432 端口防火墙
firewall-cmd --zone=public --add-port=5432/tcp --permanent
firewall-cmd --reload
- 日常开启关闭
开启systemctl start postgresql-11
重启systemctl restart postgresql-11
关闭systemctl stop postgresql-11
CentOS7 安装 PostGIS 3.1.0
-
先安装 PostgreSQL 11
-
yum install epel-release.noarch
-
yum install postgis31_11
-
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. -
创建一个叫 history_data 的数据库,并启用 PostGIS Spatial 功能
su postgres
psql -U postgres
- 如果先前修改过端口号则
psql -p 33245 -U postgres
- 若嫌烦则
- 进入 /home 创建 postgres 文件夹(若无)
- 新建 .bash_profile 文件,输入
export PGPORT=33245
( GNOME 下 ctrl + h 显示隐藏文件) su postgres
- 在 /home/postgres 中
source .bash_profile
su root
systemctl restart postgresql-11
su postgres
即可直接psql -U postgres
进入
create database "history_data"
\c "hisotory_data"
create extension postgis;
- 验证安装
select PostGIS_version();
返回3.1 USE_GEOS=1 USE_PROJ=1 USE_STATS=1
- 验证安装
select count(*) from spatial_ref_sys;
返回 8500 \q
退出 postgres 数据库用户psql -U postgres
后,连接数据库之前select * from pg_database;
查看全部数据库- 连接数据库之前
drop database "history_data";
删除数据库 su root
切回 root 用户
postgresql-11 数据搬迁至有更大存储挂载的 /hiit 下
systemctl stop postgresql-11
cp -r /var/lib/pgsql /hiit
rm -rf /hiit/pgsql/.bash_history /hiit/pgsql/.bash_profile /hiit/pgsql/.psql_history /hiit/pgsql/11/backups /hiit/pgsql/11/initdb.log
chown -R postgres.postgres /hiit/pgsql
- 修改 /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/
systemctl daemon-reload
systemctl restart postgresql-11
rpm 安装桌面版 pgadmin 4
rpm -i https://ftp.postgresql.org/pub/pgadmin/pgadmin4/yum/pgadmin4-redhat-repo-2-1.noarch.rpm
yum install -y pgadmin4-desktop
[旧] CentOS7 安装 PostGIS 2.5 教程1教程2
- 先安装 PostgreSQL 11
- 为 PostgreSQL 11 安装 PostGIS v2.5
yum install postgis25_11
(pg12对应 postgis30_11 ),若 gdal 报错则先yum install epel-release.noarch
- 验证安装,检查安装的版本
rpm -qi postgis25_11
会返回信息表 - 创建一个叫 test_postgis 的数据库,并启用 PostGIS Spatial 功能
su - postgres
psql -U postgres
create database test_postgis;
\c test_postgis
连接数据库create extension postgis;
- 验证安装
select PostGIS_version();
返回2.5 USE_GEOS=1 USE_PROJ=1 USE_STATS=1
- 验证安装
select count(*) from spatial_ref_sys;
返回 5757 \q
退出psqlsu root
切回 root 用户- 连接数据库之前
select * from pg_database;
或psql -l
查看全部数据库 - 连接数据库之前
drop database testbase;
删除数据库 - 数据库更名
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
关闭
端口占用
- 查看端口(使用
yum install lsof
安装lsof
)sudo lsof -i:8083
- 结束进程
kill (PID)
- 强制结束进程
kill -s 9 (PID)