LoginSignup
2
3

More than 3 years have passed since last update.

PostgreSQLにPostGISをセットアップする

Last updated at Posted at 2020-04-27

色々試した結果PostGISは2でも3でも、CentOS 7、PostgreSQL 12推奨です。
互換性の都合で、Amazon Linux、Amazon Linux 2は避けるべきです。
本記事は主にCentOS 7について書きますが、CentOS 8でも可能です。
既存環境で既にgdal, geos, projが入っている場合は、エラー対応項を参照してください。

OS準備

CentOS 7 (x86_64) - with Updates HVM
https://aws.amazon.com/marketplace/pp/Centosorg-CentOS-7-x8664-with-Updates-HVM/B00O7WM7QW
AWSでCentOS 7を利用する場合はこちらから、[Continue to Subscribe] -> [Continue to Configuration]を選択肢後、Choose Actionで[Launch through EC2]を選択し、インスタンスを作成します。
執筆当時の最新Software Versionは、2002_01 (Mar 16,2020)です。
セキュリティパッチが適用されている最新のものを選択すると良いと思います。

インスタンスを起動したら、pemファイルを使用しログインします。
初期ユーザーは、ec2-userではなくcentosになります。
$ ssh centos@[IPv4 パブリック IP] -i [.pemファイルのパス]

ソフトウェアインストール

初回ログイン後は、yumを最新にしてください。

sudo su
yum update -y

PostgreSQL 12

RPM Repositoriesのインストール

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

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

本体のインストール

// PostgreSQL 12 on CentOS 8
sudo dnf -qy module disable postgresql
sudo dnf -y install postgresql12 postgresql12-server

// PostgreSQL 12 on CentOS 7
sudo yum -y install epel-release yum-utils
sudo yum-config-manager --enable pgdg12
sudo yum install postgresql12-server postgresql12

PostgreSQL12 初期設定(CentOS7)

初期化

sudo /usr/pgsql-12/bin/postgresql-12-setup initdb
sudo systemctl enable --now postgresql-12
systemctl status postgresql-12

↑ statusは、Active: active (running)になっているはず。

postgresユーザーのパスワードを設定

sudo su - postgres 
psql -c "ALTER USER postgres with password 'YourPassword'"

ログイン制御

sudo vi /var/lib/pgsql/12/data/pg_hba.conf

localhostからのアクセス時にパスワード認証(MD5)をさせる場合は、下記を編集する。

local   all             all                                     peer
↓
local   all             all                                     md5

再起動後、postgresユーザーでログインできるようになる。

sudo systemctl restart postgresql-12
psql -U postgres
\q

PostGIS

sudo yum -y install epel-release

// PostGIS 2 on PostgreSQL 12
sudo yum install postgis25_12

// PostGIS 3 on PostgreSQL 12
sudo yum install postgis30_12

PostGISを有効化

PostGISがDB単位で有効化が必要です。

psql -U postgres
# CREATE USER test_user;
# CREATE DATABASE test_db OWNER test_user;
# ALTER USER test_user with password 'Test';
\q

作成したtest_dbに、super user(postgres)でログインし、

psql -U postgres -d test_db

PostGISを有効化

# CREATE EXTENSION postgis;
# SELECT PostGIS_version();

        postgis_version            
---------------------------------------
 3.0 USE_GEOS=1 USE_PROJ=1 USE_STATS=1
(1 行)

エラー対応

PostGISでは、
gcc, libxml2, gdal, geos, proj
あたりが必要になりますが、個別にインストールすると互換性の問題で、CREATE EXTENSION postgisを実行するとエラーになります。

gdal, geos, projは、pgdg-commonリポジトリのものを使いましょう。
sudo yum install postgis30_12では、gdal30-libs, geos38, proj70が入ります。

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3