Skip to content

Latest commit

 

History

History
164 lines (142 loc) · 5.51 KB

PostgreSQL+PostGISのインストール.md

File metadata and controls

164 lines (142 loc) · 5.51 KB

PostgreSQL+PostGISのインストール

PostgreSQLとPostGISのインストール

$ sudo apt install postgresql
# ...(3分程度)...
$ sudo apt install postgis
# ...(3分程度)...

PostgreSQLの動作確認とバージョンの確認

$ sudo -u postgres psql
postgres=# SELECT version();
                                                               version                                                                
--------------------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 14.10 (Ubuntu 14.10-0ubuntu0.22.04.1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0, 64-bit
(1 row)
# インストールされたバージョンを確認する。
# 2024/01/12時点の最新版: 14.10
postgres=# \q
# '\'はキーボードの右下のバックスラッシュ「ろ」を押す(右上の'¥'ではない)

PostgreSQLにログインすると、プロンプトが、

postgres=# 

に変わる。postgresの部分は接続先データベース名を表す。

設定ファイルの準備

/etc/postgresql-common/createcluster.conf

$ ls /etc/postgresql-common/
$ less /etc/postgresql-common/createcluster.conf
$ sudo cp /etc/postgresql-common/createcluster.conf /etc/postgresql-common/createcluster.conf-org
$ ls /etc/postgresql-common/

※下記の14の箇所はPostgreSQLのバージョンを表す。インストールされたバージョンに合わせて適宜置換すること。例えば、バージョン12.xをインストールした場合は、14の箇所を12に置換する。以下、同様。

/etc/postgresql/14/main/pg_hba.conf

$ ls /etc/postgresql/14/main/
$ sudo less /etc/postgresql/14/main/pg_hba.conf
$ sudo cp /etc/postgresql/14/main/pg_hba.conf /etc/postgresql/14/main/pg_hba.conf-org
$ ls /etc/postgresql/14/main/

/etc/postgresql/14/main/postgresql.conf

$ ls /etc/postgresql/14/main/
$ less /etc/postgresql/14/main/postgresql.conf
$ sudo cp /etc/postgresql/14/main/postgresql.conf /etc/postgresql/14/main/postgresql.conf-org
$ ls /etc/postgresql/14/main/

PostgreSQLサーバの起動設定

$ systemctl is-enabled postgresql
# 「enabled」と表示されれば、postgresqlの自動起動が有効になっている。
$ systemctl status postgresql
# 「Active: active (exited)」と表示されれば、postgresqlは稼働している。

PostgreSQLのパスワードの設定

$ sudo -u postgres psql
postgres=# \password
Enter new password for user "postgres":
# パスワードを入力しても表示されないが、そのまま入力する。
Enter it again: 
# パスワードを入力しても表示されないが、そのまま入力する。
postgres=# \q
$ sudo less /etc/postgresql/14/main/pg_hba.conf
$ sudo vi /etc/postgresql/14/main/pg_hba.conf

pg_hba.confの下記3箇所についてpeermd5に書き換える。viの代わりにnanoで編集しても良い。

/etc/postgresql/14/main/pg_hba.conf

...(略)...
# Database administrative login by Unix domain socket
local all postgres md5
...(略)...
# "local" is for Unix domain socket connections only
local all all md5
...(略)...
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all md5
...(略)...
$ sudo less /etc/postgresql/14/main/pg_hba.conf
$ sudo diff /etc/postgresql/14/main/pg_hba.conf-org /etc/postgresql/14/main/pg_hba.conf
$ sudo systemctl reload postgresql
$ systemctl status postgresql
$ sudo -u postgres psql
# 設定したパスワードを入力してログインできることを確認する。

参考

autovacuumの設定

$ sudo less /etc/postgresql/14/main/postgresql.conf
$ sudo vi /etc/postgresql/14/main/postgresql.conf

/etc/postgresql/14/main/postgresql.conf

...(略)...
track_counts = on
...(略)...
autovacuum = on
...(略)...
$ sudo less /etc/postgresql/14/main/postgresql.conf
$ sudo diff /etc/postgresql/14/main/postgresql.conf-org /etc/postgresql/14/main/postgresql.conf
$ sudo systemctl reload postgresql
$ systemctl status postgresql

参考

PostGISの動作テスト

$ sudo -u postgres psql
postgres=# \l
postgres=# CREATE DATABASE gistest ENCODING 'UTF8';
postgres=# \l
postgres=# \c gistest
gistest=# \d
gistest=# CREATE EXTENSION postgis;
gistest=# SELECT postgis_full_version();
gistest=# CREATE TABLE sample(id INT, name TEXT, PRIMARY KEY(id));
gistest=# CREATE TABLE gissample(id SERIAL, point GEOMETRY(POINT, 4326), line GEOMETRY(LINESTRING, 4326), polygon GEOMETRY(POLYGON, 4326));
gistest=# INSERT INTO gissample(point, line, polygon) VALUES(ST_GeomFromText('POINT(50 50)', 4326), ST_GeomFromText('LINESTRING(1 1, 99 99)', 4326), ST_GeomFromText('POLYGON((25 25, 75 25, 75 75, 25 75, 25 25))', 4326));
gistest=# SELECT ST_AsText(polygon) AS polygon FROM gissample;
gistest=# \d
gistest=# \q

参考

  • 黒い猫の開発日記, PostGISを利用してみる。
    • ただし、GeomFromTextST_GeomFromTextに、AsTextST_AsTextに読み替える必要がある。