centos7使用yum安装postgis数据库

发布时间 2023-05-06 15:07:07作者: james-roger
https://www.postgresql.org/download/linux/redhat/
1 添加PostgreSQL Yum源

sudo yum install epel-release
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

2 安装PostgreSQL和PostGIS
sudo yum install postgresql12-server postgresql12-contrib postgis30_12

3 初始化PostgreSQL数据库
sudo /usr/pgsql-12/bin/postgresql-12-setup initdb

4 启动PostgreSQL服务
sudo systemctl start postgresql-12
sudo systemctl enable postgresql-12

5 创建PostgreSQL数据库
sudo su - postgres
createdb mydatabase

6 将PostGIS扩展添加到数据库中
psql -d mydatabase -c "CREATE EXTENSION postgis;"
psql -d mydatabase -c "CREATE EXTENSION postgis_topology;"

  

 

开启远程访问

修改配置文件

配置文件目录 /var/lib/pgsql/12/data/ 如果是安装的postgres 12  则 目录是 12

1.修改配置文件postgresql.conf

listen_addresses = ‘localhost‘取消注释,更改为:listen_addresses = ‘*‘ 

2.pg_hba.conf

在该配置文件的host all all 127.0.0.1/32 trust行下添加以下配置,或者直接将这一行修改为以下配置

host    all    all    0.0.0.0/0    trust

用工具连接上数据库后,修改数据库的密码

ALTER USER postgres WITH PASSWORD 'postgres';

然后再将上面的trust改成md5重启数据

最后重启postgres

关闭postgres服务的命令 sudo systemctl stop postgresql-12
启动postgres服务的命令 sudo systemctl start postgresql-12

postgres的安装目录/usr/pgsql-12 里面有lib和bin目录

 

创建空间数据表

 

使用命令方式

CREATE TABLE my_test_table (
  id SERIAL PRIMARY KEY,
  geom GEOMETRY(Point, 4326),
  name VARCHAR(128)
);

  

INSERT into table_point(name, geom) VALUES ('测试',ST_GeomFromText('POINT(112.22 30.12)', 4326))

SELECT name,st_astext(geom) FROM table_point