Postgre和拓展PostGIS 搭建和配置

目录:

  1. 搭建过程
  2. 安装PostGIS

搭建过程

  1. 安装PG

    [root@localhost ~]# yum install readline* -y
    [root@localhost ~]# wget https://ftp.postgresql.org/pub/source/v9.5.1/postgresql-9.5.1.tar.gz
    [root@localhost ~]# tar xvf postgresql-9.5.1.tar.gz
    [root@localhost ~]# cd postgresql-9.5.1/
    [root@localhost postgresql-9.5.1]# ./configure --prefix=/usr/local/pgsql --without-zlib
    [root@localhost postgresql-9.5.1]# make world && make install-world 
    
  2. 创建用户及用户组

    [root@localhost postgresql-9.5.1]# groupadd -g 101 dba
    [root@localhost postgresql-9.5.1]# useradd -u 516 -g dba -G root -d /usr/local/pgsql postgres
    [root@localhost postgresql-9.5.1]# id postgres
    uid=516(postgres) gid=101(dba) groups=101(dba),0(root)
    [root@localhost postgresql-9.5.1]# passwd postgres
    
  3. 配置.bash_profile

    [root@localhost ~]# cp /etc/skel/.* /usr/local/pgsql/
    [root@localhost skel]# cd /usr/local/pgsql/
    
    [root@localhost pgsql]# vim .bash_profile 
    # .bash_profile
    
    # Get the aliases and functions
    if [ -f ~/.bashrc ]; then
            . ~/.bashrc
    fi
    
    # User specific environment and startup programs
    
    PATH=$PATH:$HOME/bin
    
    export PGHOME=/usr/local/pgsql
    
    export PGDATA=/usr/local/pgsql/data
    
    export PATH=$PATH:/usr/local/pgsql/bin
    
    #stty erase ^H
    set umask to 022
    umask 022
    PS1=`uname -n`":"'$USER'":"'$PWD'":>"; export PS1
    
  4. 创建数据目录并修改权限

    [root@localhost pgsql]# mkdir data
    [root@localhost pgsql]# chmod -R 755 /usr/local/pgsql/
    [root@localhost pgsql]# chmod -R 700 /usr/local/pgsql/data
    [root@localhost pgsql]# chown -R postgres.dba /usr/local/pgsql/
    
  5. 配置服务并设置开机启动

    [root@localhost postgresql-9.5.1]# cp contrib/start-scripts/linux /etc/init.d/postgresql
    [root@localhost postgresql-9.5.1]# chmod +x /etc/init.d/postgresql
    [root@localhost postgresql-9.5.1]# chkconfig --del postgresql
    [root@localhost postgresql-9.5.1]# chkconfig --add postgresql
    [root@localhost postgresql-9.5.1]# chkconfig postgresql on
    [root@localhost postgresql-9.5.1]# chkconfig | grep post
    
    Note: This output shows SysV services only and does not include native
          systemd services. SysV configuration data might be overridden by native
          systemd configuration.
    
          If you want to list systemd services use 'systemctl list-unit-files'.
          To see services enabled on particular target use
          'systemctl list-dependencies [target]'.
    
    postgresql     	0:off	1:off	2:on	3:on	4:on	5:on	6:off
    
  6. 初始化db并启动服务

    [root@localhost postgresql-9.5.1]# su - postgres
    localhost.localdomain:postgres:/usr/local/pgsql:>initdb -D /usr/local/pgsql/data
    The files belonging to this database system will be owned by user "postgres".
    This user must also own the server process.
    
    The database cluster will be initialized with locale "en_US.UTF-8".
    The default database encoding has accordingly been set to "UTF8".
    The default text search configuration will be set to "english".
    
    Data page checksums are disabled.
    
    fixing permissions on existing directory /usr/local/pgsql/data ... ok
    creating subdirectories ... ok
    selecting default max_connections ... 100
    selecting default shared_buffers ... 128MB
    selecting dynamic shared memory implementation ... posix
    creating configuration files ... ok
    creating template1 database in /usr/local/pgsql/data/base/1 ... ok
    initializing pg_authid ... ok
    initializing dependencies ... ok
    creating system views ... ok
    loading system objects' descriptions ... ok
    creating collations ... ok
    creating conversions ... ok
    creating dictionaries ... ok
    setting privileges on built-in objects ... ok
    creating information schema ... ok
    loading PL/pgSQL server-side language ... ok
    vacuuming database template1 ... ok
    copying template1 to template0 ... ok
    copying template1 to postgres ... ok
    syncing data to disk ... ok
    
    WARNING: enabling "trust" authentication for local connections
    You can change this by editing pg_hba.conf or using the option -A, or
    --auth-local and --auth-host, the next time you run initdb.
    
    Success. You can now start the database server using:
    
        pg_ctl -D /usr/local/pgsql/data -l logfile start
    
    
    localhost.localdomain:postgres:/usr/local/pgsql:>pg_ctl start
    server starting
    localhost.localdomain:postgres:/usr/local/pgsql:>LOG:  database system was shut down at 2020-01-30 19:36:17 CST
    LOG:  MultiXact member wraparound protections are now enabled
    LOG:  database system is ready to accept connections
    LOG:  autovacuum launcher started
    
    
    [root@localhost postgresql-9.5.1]# netstat -tlnp | grep postgres
    tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      25432/postgres      
    tcp6       0      0 ::1:5432                :::*                    LISTEN      25432/postgres   
    
  7. 创建数据库用户与数据库

    ocalhost.localdomain:postgres:/usr/local/pgsql:>psql
    psql (9.5.1)
    Type "help" for help.
    
    postgres=# create user testadmin password 'testadmin123' login;
    CREATE ROLE
    postgres=# create database testdb with owner=testadmin;
    CREATE DATABASE
    postgres=# \q
    localhost.localdomain:postgres:/usr/local/pgsql:>pg_ctl status
    pg_ctl: server is running (PID: 25432)
    /usr/local/pgsql/bin/postgres
    

回到目录

安装PostGIS

  1. 准备环境

    [root@localhost ~]# yum -y install gcc gcc+ gcc-c++
    [root@localhost ~]# yum -y install libxml2 libxml2-devel
    [root@localhost ~]# mkdir -p /usr/local/pgsql/plugin
    [root@localhost ~]# cd /usr/local/pgsql/plugin
    
  2. 安装 proj4

    [root@localhost plugin]# wget http://download.osgeo.org/proj/proj-4.8.0.tar.gz
    [root@localhost plugin]#wget http://download.osgeo.org/proj/proj-4.8.0.tar.gz
    [root@localhost plugin]# tar zxvf proj-4.8.0.tar.gz
    [root@localhost plugin]# cd proj-4.8.0
    [root@localhost proj-4.8.0]#./configure --prefix=/usr/local/pgsql/plugin/proj
    [root@localhost proj-4.8.0]#make && make install
    
    [root@localhost proj-4.8.0]# vim /etc/ld.so.conf.d/proj-4.8.0.conf
    /usr/local/pgsql/plugin/proj/lib
    [root@localhost proj-4.8.0]# ldconfig
    
  3. 安装geos

    [root@localhost ~]# cd /usr/local/pgsql/plugin/
    [root@localhost plugin]# wget http://download.osgeo.org/geos/geos-3.4.2.tar.bz2
    [root@localhost plugin]# bzip2 -d geos-3.4.2.tar.bz2 
    [root@localhost plugin]# tar -xf geos-3.4.2.tar 
    [root@localhost plugin]# cd geos-3.4.2/
    [root@localhost geos-3.4.2]# ./configure --prefix=/usr/local/pgsql/plugin/geos
    [root@localhost geos-3.4.2]# make && make install
    
    [root@localhost geos-3.4.2]# vim /etc/ld.so.conf.d/geos-3.4.2.conf
    /usr/local/pgsql/plugin/geos/lib
    [root@localhost geos-3.4.2]# ldconfig
    
  4. 安装GDAL

    [root@localhost geos-3.4.2]# cd ..
    [root@localhost plugin]# wget http://download.osgeo.org/gdal/2.0.1/gdal-2.0.1.tar.gz
    [root@localhost plugin]# tar xf gdal-2.0.1.tar.gz 
    [root@localhost plugin]# cd gdal-2.0.1/
    [root@localhost gdal-2.0.1]# ./configure --prefix=/usr/local/pgsql/plugin/gdal
    [root@localhost gdal-2.0.1]# make && make install
    
    [root@localhost gdal-2.0.1]# vim /etc/ld.so.conf.d/gdal-2.0.1.conf
    /usr/local/pgsql/plugin/gdal/lib
    [root@localhost gdal-2.0.1]# ldconfig
    
  5. 安装PostGIS

    [root@localhost gdal-2.0.1]# cd ..
    [root@localhost plugin]# wget http://download.osgeo.org/postgis/source/postgis-2.1.8.tar.gz
    [root@localhost plugin]# tar xf postgis-2.1.8.tar.gz 
    [root@localhost plugin]# cd postgis-2.1.8/
    [root@localhost postgis-2.1.8]# ./configure --prefix=/usr/local/pgsql/plugin/PostGIS \
    > --with-pgconfig=/usr/local/pgsql/bin/pg_config \
    > --with-geosconfig=/usr/local/pgsql/plugin/geos/bin/geos-config \
    > --with-gdalconfig=/usr/local/pgsql/plugin/gdal/bin/gdal-config \
    > --with-projdir=/usr/local/pgsql/plugin/proj/
    > [root@localhost postgis-2.1.8]# make && make install
    
  6. 检查安装是否成功

    [root@localhost ~]# su - postgres
    Last login: Wed Feb 26 17:07:52 CST 2020
    localhost.localdomain:postgres:/usr/local/pgsql:>psql 
    psql (9.5.1)
    Type "help" for help.
    
    postgres=# select * from pg_available_extensions where name like 'postgis%';
              name          | default_version | installed_version |                               comment                               
    ------------------------+-----------------+-------------------+---------------------------------------------------------------------
     postgis                | 2.1.8           |                   | PostGIS geometry, geography, and raster spatial types and functions
     postgis_tiger_geocoder | 2.1.8           |                   | PostGIS tiger geocoder and reverse geocoder
     postgis_topology       | 2.1.8           |                   | PostGIS topology spatial types and functions
    (3 rows)
    
    postgres=# CREATE EXTENSION PostGIS;
    CREATE EXTENSION
    postgres=# CREATE EXTENSION postgis_topology;
    CREATE EXTENSION
    postgres=# CREATE EXTENSION  fuzzystrmatch;
    CREATE EXTENSION
    postgres=# CREATE EXTENSION postgis_tiger_geocoder;
    CREATE EXTENSION
    

回到目录

你可能感兴趣的:(数据库)