diadia

興味があることをやってみる。自分のメモを残しておきます。

geodjangoのためにcentos7でpostgis環境を構築する

まずCentos7にpostgresql11をインストールする

postgeSQL公式のリポジトリを利用する。

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

リポジトリをインストール後postgresqlをインストールする。今回はpostgresql11を利用する。
postgresql11-serverのインストールで依存するパッケージもインストールされる。

# yum install postgresql11-server
( 補足 )yum install postgreql-serverにするとcentos7備え付けのpostgresqlをインストールすることになるので気をつけること

そうすると以下のような依存関係にあるものも一緒にインストールされる。

Installed:
  postgresql11-server.x86_64 0:11.3-1PGDG.rhel7                                                                      

Dependency Installed:
  libicu.x86_64 0:50.1.2-17.el7  postgresql11.x86_64 0:11.3-1PGDG.rhel7  postgresql11-libs.x86_64 0:11.3-1PGDG.rhel7 

postGISのライブラリをインストールする

参考:https://qiita.com/SOJO/items/216b6897104354a0c3fb

EPELのインストール

# yum install epel-release

Proj とGDALのインストール

# yum install -y proj proj-devel proj-epsg
# yum --enablerepo=epel install gdal

PostGIS のインストールをおこなう。入手できる postgis のパッケージを確認する

# yum list | grep postgis
postgis.x86_64                            2.0.7-2.el7                    epel   
postgis-docs.x86_64                       2.0.7-2.el7                    epel   
postgis-utils.x86_64                      2.0.7-2.el7                    epel   
postgis22_94.x86_64                       2.2.7-1.rhel7                  pgdg94 
postgis22_94-client.x86_64                2.2.7-1.rhel7                  pgdg94 
postgis22_94-debuginfo.x86_64             2.2.6-1.rhel7                  pgdg94 
postgis22_94-devel.x86_64                 2.2.7-1.rhel7                  pgdg94 
postgis22_94-docs.x86_64                  2.2.7-1.rhel7                  pgdg94 
postgis22_94-utils.x86_64                 2.2.7-1.rhel7                  pgdg94 
postgis22_95.x86_64                       2.2.7-1.rhel7                  pgdg95 
postgis22_95-client.x86_64                2.2.7-1.rhel7                  pgdg95 
postgis22_95-debuginfo.x86_64             2.2.6-1.rhel7                  pgdg95 
postgis22_95-devel.x86_64                 2.2.7-1.rhel7                  pgdg95 
postgis22_95-docs.x86_64                  2.2.7-1.rhel7                  pgdg95 
postgis22_95-utils.x86_64                 2.2.7-1.rhel7                  pgdg95 
...割愛...
postgis30_11.x86_64                       3.0.0-1.rhel7                  pgdg11 
postgis30_11-client.x86_64                3.0.0-1.rhel7                  pgdg11 
postgis30_11-debuginfo.x86_64             3.0.0-1.rhel7                  pgdg11 
postgis30_11-devel.x86_64                 3.0.0-1.rhel7                  pgdg11 
postgis30_11-docs.x86_64                  3.0.0-1.rhel7                  pgdg11 
postgis30_11-gui.x86_64                   3.0.0-1.rhel7                  pgdg11 
postgis30_11-utils.x86_64                 3.0.0-1.rhel7                  pgdg11 
postgis30_12.x86_64                       3.0.0-1.rhel7                  pgdg12 
postgis30_12-client.x86_64                3.0.0-1.rhel7                  pgdg12 
postgis30_12-debuginfo.x86_64             3.0.0-1.rhel7                  pgdg12 
postgis30_12-devel.x86_64                 3.0.0-1.rhel7                  pgdg12 
postgis30_12-docs.x86_64                  3.0.0-1.rhel7                  pgdg12 
postgis30_12-gui.x86_64                   3.0.0-1.rhel7                  pgdg12 
postgis30_12-utils.x86_64                 3.0.0-1.rhel7                  pgdg12 
postgis30_95.x86_64                       3.0.0-1.rhel7                  pgdg95 
postgis30_95-client.x86_64                3.0.0-1.rhel7                  pgdg95 
postgis30_95-debuginfo.x86_64             3.0.0-1.rhel7                  pgdg95 
postgis30_95-devel.x86_64                 3.0.0-1.rhel7                  pgdg95 
postgis30_95-docs.x86_64                  3.0.0-1.rhel7                  pgdg95 
postgis30_95-gui.x86_64                   3.0.0-1.rhel7                  pgdg95 
postgis30_95-utils.x86_64                 3.0.0-1.rhel7                  pgdg95 
postgis30_96.x86_64                       3.0.0-1.rhel7                  pgdg96 
postgis30_96-client.x86_64                3.0.0-1.rhel7                  pgdg96 
postgis30_96-debuginfo.x86_64             3.0.0-1.rhel7                  pgdg96 
postgis30_96-devel.x86_64                 3.0.0-1.rhel7                  pgdg96 
postgis30_96-docs.x86_64                  3.0.0-1.rhel7                  pgdg96 
postgis30_96-gui.x86_64                   3.0.0-1.rhel7                  pgdg96 
postgis30_96-utils.x86_64                 3.0.0-1.rhel7                  pgdg96 

postgis30_11 というのが PostgreSQL11 に対応したものである。これをインストールする。

yum install postgis30_11.x86_64

データクラスター($PGDATA)を作成する

データクラスターを作成するためにはPostgreSQLの初期化する。初期化すると自動的にデータクラスターを作成される。 参考:https://www.postgresql.jp/document/10/html/creating-cluster.html 初期化するにはinitdbコマンドを入力する必要がある。しかしながらinitdbへのパスが通っていないので以下のようにしてinitdbコマンドを入力する。

# initdbの実行
# /usr/pgsql-11/bin/postgresql-11-setup initdb

Initializing database ... OK

インストールするとpostgresユーザが自動的に作成される。このpostgresユーザのパスワードを設定する。パスワードの設定はos(centos7)のユーザ切り替え用のパスワードの設定とpostgresqlに接続するためのパスワードの設定がある。

# osユーザ切り替えのためのパスワード設定

sudo passwd postgres

Changing password for user postgres.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.

ちなみにpostgresql11の場合データベースクラスターは以下のパスに作成される。

/var/lib/pgsql/11/data

参考:https://qiita.com/zabutonzabuzabu/items/070a7f833bb321e46b0e

# su postgres
psql -U postgres

ALTER ROLE postgres WITH PASSWORD '設定したいpostgresのパスワード';


# データベースの作成
CREATE DATABASE [database名] ;

# ユーザを作成する
CREATE USER [ユーザ名] WITH PASSWORD '[パスワード]' ;

ALTER ROLE [ユーザ名] SET client_encoding TO 'utf8' ;

ALTER ROLE [ユーザ名] SET default_transaction_isolation TO 'read committed';

ALTER ROLE [ユーザ名] SET timezone TO 'Asia/Tokyo';

# このセット内容をデータベースに反映させるコマンドをが以下のもの。
GRANT ALL PRIVILEGES ON DATABASE [database名] TO [ユーザ名];
\q




vi /var/lib/pgsql/11/data/pg_hba.conf

#認証方法をpeerからmd5に変更
local   all     all     md5
# systemctl start postgresql-11
# systemctl enable postgresql-11


su postgres
psql -U postgres -d postgis_test;

postgis_test=# CREATE EXTENSION postgis;
CREATE EXTENSION

postgis_test=# SELECT PostGIS_full_version();