3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ubuntu16にPostGISを導入するまでの流れ

Last updated at Posted at 2019-05-09

初めに

所用でPostgreSQLとPostGISを使う必要が出てきたので使えるように初期設定するのを自分なりにまとめてみました。
まだ模索中の部分もあるため誤り、不足など教えていただけると幸いです。

環境

Windows10Pro 1809にWSLで入れたUbuntu16.04.01

PostgreSQLのインストール

この部分は色々なサイトで上がっていると思われるので簡単にやっていきます。

$ sudo apt install postgresql
$ sudo apt install postgresql-9.5-postgis-2.2

これでPostgreSQLとPostGISのインストールは終わりました。

もし、最新版のPostgreSQLを使用したい場合はリポジトリの追加を行います。
作成時(2019/05/09)での最新版は
PostgreSQL : 11
PostGIS : 2.5
です。
このサイトを参考にしてやると

$ wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
$ sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -sc)-pgdg main" > /etc/apt/sources.list.d/PostgreSQL.list'
$ sudo apt update
$ sudo apt install-postgresql-11
$ sudo apt install-postgresql-11-postgis-2.5

となります。

非同期設定をoffにする(WSL限定)

どうやら現在のWSLだとPostgreSQLの非同期設定がONの状態ではPostgreSQLに接続できないらしい(ここ参照)
そのため非同期設定をOFFにします。
sudo vim /etc/postgresql/11/main/postgresql.conf などでpostgresql.confを開き、最下部に

fsync = false

を追加します。
これで非同期設定がOFFになり接続できるようになります。

PostgreSQLの認証を変更する

インストール時の設定だとローカルのユーザー名、パスワードと同じものでしかログインできません。
そこで現在のローカルユーザー以外のユーザーでもログインできるようにします。
ここを参考にして

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            peer

# "local" is for Unix domain socket connections only
local   all             all                                     md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5

にします。そうするとユーザー名を指定してのログインが可能になります。

Postgresユーザーのパスワード作成

PotgreSQLインストール時に作成されたユーザーはそのままだとパスワードがありません。
ログイン時の安全のためにもパスワードを作成します。

$ sudo passwd postgres

で postgresユーザーのパスワードの設定ができます。
現在のパスワードが求められた場合は、パスワードがpostgresになっている可能性があります。

データベースの作成

これでPostgreSQLにログインできるようになりましたのでログインしてデータベースを作成します。
まずsudo service postgresql startでPostgreSQLを起動させます。
次にsudo su - postgresでpostgresユーザーに変更してpsqlコマンドでPostfgreSQLにログインします。
ログインできたのでデータベースを作成します。

postgres=# create database gis encoding 'UTF8';

(今回はデータベース名をgisにしておきます。)
データベース名の後ろのオプションは文字コードをUTF-8にするためのものです。

これでデータベースは作成できたので次に拡張機能を入れます。
この拡張機能のことがPostGISなのです。

postgres=# \c gis
gis=# create extension postgis;
gis=# create extension postgis_topology;

これで基本的なPostGISの拡張機能は導入されました。
他にも拡張機能はあるので必要になったら入れると良いでしょう。

確認

これでPostGISの導入に成功しました。
確認のため現在のデータベースに導入されている拡張機能を見ておきましょう。

gis=# select * from pg_available_extensions;
             name             | default_version | installed_version |                                                       comment                                                       
------------------------------+-----------------+-------------------+---------------------------------------------------------------------------------------------------------------------
 address_standardizer         | 2.5.2           |                   | Used to parse an address into constituent elements. Generally used to support geocoding address normalization step.
 address_standardizer_data_us | 2.5.2           |                   | Address Standardizer US dataset example
 adminpack                    | 2.0             |                   | administrative functions for PostgreSQL
 amcheck                      | 1.1             |                   | functions for verifying relation integrity
 autoinc                      | 1.0             |                   | functions for autoincrementing fields
 bloom                        | 1.0             |                   | bloom access method - signature file based index
 btree_gin                    | 1.3             |                   | support for indexing common datatypes in GIN
 btree_gist                   | 1.5             |                   | support for indexing common datatypes in GiST
 citext                       | 1.5             |                   | data type for case-insensitive character strings
 cube                         | 1.4             |                   | data type for multidimensional cubes
 dblink                       | 1.2             |                   | connect to other PostgreSQL databases from within a database
 dict_int                     | 1.0             |                   | text search dictionary template for integers
 dict_xsyn                    | 1.0             |                   | text search dictionary template for extended synonym processing
 earthdistance                | 1.1             |                   | calculate great-circle distances on the surface of the Earth
 file_fdw                     | 1.0             |                   | foreign-data wrapper for flat file access
 fuzzystrmatch                | 1.1             |                   | determine similarities and distance between strings
 hstore                       | 1.5             |                   | data type for storing sets of (key, value) pairs
 insert_username              | 1.0             |                   | functions for tracking who changed a table
 intagg                       | 1.1             |                   | integer aggregator and enumerator (obsolete)
 intarray                     | 1.2             |                   | functions, operators, and index support for 1-D arrays of integers
 isn                          | 1.2             |                   | data types for international product numbering standards
 lo                           | 1.1             |                   | Large Object maintenance
 ltree                        | 1.1             |                   | data type for hierarchical tree-like structures
 moddatetime                  | 1.0             |                   | functions for tracking last modification time
 pageinspect                  | 1.7             |                   | inspect the contents of database pages at a low level
 pg_buffercache               | 1.3             |                   | examine the shared buffer cache
 pg_freespacemap              | 1.2             |                   | examine the free space map (FSM)
 pg_prewarm                   | 1.2             |                   | prewarm relation data
 pg_stat_statements           | 1.6             |                   | track execution statistics of all SQL statements executed
 pg_trgm                      | 1.4             |                   | text similarity measurement and index searching based on trigrams
 pg_visibility                | 1.2             |                   | examine the visibility map (VM) and page-level visibility info
 pgcrypto                     | 1.3             |                   | cryptographic functions
 pgrowlocks                   | 1.2             |                   | show row-level locking information
 pgstattuple                  | 1.5             |                   | show tuple-level statistics
 plpgsql                      | 1.0             | 1.0               | PL/pgSQL procedural language
 postgis                      | 2.5.2           | 2.5.2             | PostGIS geometry, geography, and raster spatial types and functions
 postgis_sfcgal               | 2.5.2           |                   | PostGIS SFCGAL functions
 postgis_tiger_geocoder       | 2.5.2           |                   | PostGIS tiger geocoder and reverse geocoder
 postgis_topology             | 2.5.2           | 2.5.2             | PostGIS topology spatial types and functions
 postgres_fdw                 | 1.0             |                   | foreign-data wrapper for remote PostgreSQL servers
 refint                       | 1.0             |                   | functions for implementing referential integrity (obsolete)
 seg                          | 1.3             |                   | data type for representing line segments or floating-point intervals
 sslinfo                      | 1.2             |                   | information about SSL certificates
 tablefunc                    | 1.0             |                   | functions that manipulate whole tables, including crosstab
 tcn                          | 1.0             |                   | Triggered change notifications
 timetravel                   | 1.0             |                   | functions for implementing time travel
 tsm_system_rows              | 1.0             |                   | TABLESAMPLE method which accepts number of rows as a limit
 tsm_system_time              | 1.0             |                   | TABLESAMPLE method which accepts time in milliseconds as a limit
 unaccent                     | 1.1             |                   | text search dictionary that removes accents
 uuid-ossp                    | 1.1             |                   | generate universally unique identifiers (UUIDs)

しっかりinstalled_versionのところに先ほど導入したpostgis、postgis_topologyがありますね。
これで導入は完了しました。

まとめ

これでUbuntuにPostGISを導入することが出来ました。
これにshpから空間情報を入れたり、Qgisから接続してデータを利用したりすることが出来ます。
そちらについての方法も作成するか検討中です。
ここまで読んでいただきありがとうございました。

3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?