PostgreSQL エクスポートとインポート

PostgreSQLのテーブル定義とデータをエクスポート(出力)とインポート(入力)するサンプルです。
(確認環境:Windows 10,PostgreSQL 11)

目次

サンプル PostgreSQLからエクスポートする
  PostgreSQLにインポートする

PostgreSQLからエクスポートする

pg_dump [connection-option...] [option...] [dbname]
  • PostgreSQLのテーブル定義とデータをローカルにテキストファイルで出力します。
  • コマンドプロンプト上でPostgreSQLにログインしていない状態で実行します。
  • pg_dumpコマンドを実行後、パスワードを入力します。
  • 既にファイルがある場合は上書きされます。
  • 以下は、PostgreSQLのpg_dumpのリンクです。オプションは多数あります。
    https://www.postgresql.jp/docs/9.0/app-pgdump.html

実行例

pg_dump.exeがあるフォルダに移動しコマンドプロンプトで以下のコマンドを実行します。
サンプルでは、D:\Program Files\PostgreSQL\11\binに移動しました。

pg_dump -h localhost -p 5432 -U postgres -d testdb10 > D:\Test1\db1.txt
パスワード:

1行目は、-dはデータベースでtestdb10データベースを指定しています。-hはホスト、-pはポート、-Uはユーザです。
2行目は、パスワードを入力します。

出力結果

以下は、出力ファイル(db1.txt)の内容です。

<前略>
--
-- Name: employee; Type: TABLE; Schema: public; Owner: postgres
--
CREATE TABLE public.employee (
    id integer NOT NULL,
    name character varying(20) NOT NULL,
    romaji character varying(20),
    created_at timestamp without time zone,
    updated_at timestamp without time zone
);

ALTER TABLE public.employee OWNER TO postgres;
<中略>
--
-- Data for Name: employee; Type: TABLE DATA; Schema: public; Owner: postgres
--
COPY public.employee (id, name, romaji, created_at, updated_at) FROM stdin;
1	鈴木	suzuki	2022-01-16 17:03:20.916	2022-01-16 17:03:20.916
2	田中	tanaka	2022-01-16 17:03:20.916	2022-01-16 17:03:20.916
3	佐藤	sato	2022-01-16 17:03:20.916	2022-01-16 17:03:20.916
\.
<後略>

5行目は、create文です。テーブルを作成します。
18行目は、上記のテーブルにデータをセットします。

 

PostgreSQLにインポートする

COPY table_name [ ( column_name [, ...] ) ]
FROM { 'filename' | PROGRAM 'command' | STDIN }
[ [ WITH ] ( option [, ...] ) ]
  • テーブルの作成はcreate文を使用し、データのインポートはCOPYを使用します。
    →作成する場合は、エクスポートしたファイルを元にすると楽です。
  • コマンドプロンプト上でPostgreSQLにログインしていない状態で実行します。
  • PostgreSQLコマンドを実行後、パスワードを入力します。
  • 以下は、PostgreSQLのCOPYのリンクです。オプションは他にも多数あります。
    https://www.postgresql.jp/document/9.4/html/sql-copy.html

実行例

psql.exeがあるフォルダに移動しコマンドプロンプトで以下のコマンドを実行します。
サンプルでは、D:\Program Files\PostgreSQL\11\binに移動しました。

psql -h localhost -p 5432 -U postgres -d testdb10 < D:\Test1\db2.txt
Enter password:

1行目は、-dはデータベースでtestdb10データベースを指定しています。-hはホスト、-pはポート、-Uはユーザです。
2行目は、パスワードを入力します。

入力ファイル例

以下は、入力ファイル(db2.txt)の内容です。

エクスポートしたファイルを元にしています。
テーブルの作成、データのインポート、キーの設定をしています。

--
-- PostgreSQL database dump
--

-- Dumped from database version 11.5
-- Dumped by pg_dump version 11.5

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

SET default_tablespace = '';

SET default_with_oids = false;

--
-- Name: employee; Type: TABLE; Schema: public; Owner: postgres
--

CREATE TABLE public.employee (
    id integer NOT NULL,
    name character varying(20) NOT NULL,
    romaji character varying(20),
    created_at timestamp without time zone,
    updated_at timestamp without time zone
);

ALTER TABLE public.employee OWNER TO postgres;

--
-- Data for Name: employee; Type: TABLE DATA; Schema: public; Owner: postgres
--

COPY public.employee (id, name, romaji, created_at, updated_at) FROM stdin;
1	鈴木	suzuki	2022-01-16 17:03:20.916	2022-01-16 17:03:20.916
2	田中	tanaka	2022-01-16 17:03:20.916	2022-01-16 17:03:20.916
3	佐藤	sato	2022-01-16 17:03:20.916	2022-01-16 17:03:20.916
\.

--
-- Name: employee employee_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
--

ALTER TABLE ONLY public.employee
    ADD CONSTRAINT employee_pkey PRIMARY KEY (id);

--
-- PostgreSQL database dump complete
--

関連の記事

PostgreSQL DDL文でtable,view等を作成する

△上に戻る