5
5

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 3 years have passed since last update.

住所データをPostgreSQLに取り込む方法例

Posted at

オープンデータとして公開されている Geolonia 住所データ を PostgreSQL に取り込む方法の一例を紹介します。

① 住所データを格納するテーブルを作成します。

CREATE TABLE "住所データ" (
  "都道府県コード" text,
  "都道府県名" text,
  "都道府県名カナ" text,
  "都道府県名ローマ字" text,
  "市区町村コード" text,
  "市区町村名" text,
  "市区町村名カナ" text,
  "市区町村名ローマ字" text,
  "大字町丁目コード" text,
  "大字町丁目名" text,
  "緯度" numeric,
  "経度" numeric
);

※テーブル名や列名、データ型などは適宜適切なものに変更してください。

\copy ... program コマンドで住所データをテーブルにロードします。

\copy "住所データ" from program 'curl -s https://raw.githubusercontent.com/geolonia/japanese-addresses/master/data/latest.csv' with (format csv, header on);

③ 住所データが取り込まれたのを確認します。

SELECT count(*) FROM "住所データ";
 count  
--------
 189539
(1 row)

SELECT "都道府県名", count(*) FROM "住所データ" GROUP BY "都道府県名" ORDER BY count DESC LIMIT 5;
 都道府県名 | count 
------------+-------
 北海道     | 25914
 愛知県     | 14507
 兵庫県     |  8967
 大阪府     |  8603
 京都府     |  7631
(5 rows)
5
5
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
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?