ubuntu20.04 在线安装postgresql 扩展postgis
基础配置 /etc/apt/sources.list
# 添加pg官方基础配置
deb http://apt.postgresql.org/pub/repos/apt/ focal-pgdg main
# 添加ubuntu官方依赖(防止下载依赖错误)
deb http://archive.ubuntu.com/ubuntu/ focal main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ focal-backports main restricted universe multiverse
deb http://security.ubuntu.com/ubuntu/ focal-security main restricted universe multiverse
环境确认命令
#更新依赖
1、apt update
#查看是否有对应安装版本
1、sudo apt-cache search postgis
2、sudo apt-cache search postgresql
开始安装
1、sudo apt-get install postgresql-16
【我的环境缺失依赖:sudo apt install postgresql-client-16 libicu66 libldap-2.4-2 libllvm10 libpq5 libssl1.1】
2、sudo apt-get install postgresql-16-postgis-3
修改默认创建postgres账户密码
1、sudo passwd postgres
2、切换到postgres账户执行psql,对数据库管理员密码进行修改
3、alter user postgres with password 'postgres';
修改远程访问
1、/etc/postgresql/16/main/postgresql.conf 修改监听IP的范围
修改监听范围
listen_addresses = 'localhost'
改为
listen_addresses = '*'
2、pg_hba.conf 给远程访问权限
/etc/postgresql/16/main/pg_hba.conf
添加
host all all 0.0.0.0/0 md5
数据库扩展函数库
CREATE EXTENSION postgis;
CREATE EXTENSION fuzzystrmatch;
CREATE EXTENSION postgis_tiger_geocoder;
CREATE EXTENSION address_standardizer;
CREATE EXTENSION postgis_topology;
数据库验证语句
SELECT ST_AsText(ST_SetSRID(ST_MakePoint(116.4074, 39.9042), 4326)) ;
前后结合重点数据库函数
ST_AsGeoJSON ( st_astext ( st_transform(geom,4326) ) )
数据库备份脚本脚本.sh
#!/bin/bash
cur_time=$(date '+%Y-%m-%d')
PGPASSWORD=postgres pg_dump -Ft -U postgres -p 5432 -h 10.10.64.44 -d gmhlc -n dl-gmhlc > "/var/lib/postgresql/backup/backup_dl_gmhlc.$cur_time.tar"
PGPASSWORD=postgres pg_dump -Ft -U postgres -p 5432 -h 10.10.64.44 -d lyt -n public > "/var/lib/postgresql/backup/backup_lyt_public.$cur_time.tar"
find /var/lib/postgresql/backup/ -mtime +10 -name "*.tar" -exec rm -rf {} \;
创建定时任务exe_backdata.cron
0 2 * * * /var/lib/postgresql/backup/脚本.sh 2>> /var/lib/postgresql/backup/log/日志.log
系统启动定时任务
crontab exe_backdata.cron
crontab -l
结束