テーブルデータをファイル/csvにエクスポートする方法 - PostgreSQL

テーブルデータをファイル/ csvにエクスポートする方法– PostgreSQL

PostgreSQLには、PostgreSQLデータベースからデータをエクスポートするための使いやすいエクスポートツールが付属しています。 このチュートリアルでは、PostgreSQLからファイルまたはcsvファイルにデータをエクスポートする方法を示します。

1. PostgreSQLを接続する

psqlコマンドを使用して、PostgreSQLデータベースに接続します。

$ psql -p 5433 -U dba dbname

P.S 5433 is my PostgreSQL port number.

2. 輸出準備完了

\o /home/yongmo/data25000.csv」と入力すると、次のクエリ結果がファイル「/home/yongmo/data25000.csv」にエクスポートされることがPostgreSQLに通知されます。

dbname=> \o /home/yongmo/data25000.csv

3. エクスポートするクエリ

次に、通常のクエリを発行します。

dbname=> select url from urltable where scoreid=1 limit 25000;

クエリの結果全体が/home/yongmo/data25000.csvにエクスポートされます。

4. 完全な例

完全なコマンドは次のとおりです。

yongmo@abcdb:~$ psql -p 5433 -U dba dbname
Password for user dba:
Welcome to psql 8.2.4 (server 8.3.3), the PostgreSQL interactive terminal.

Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with psql commands
       \g or terminate with semicolon to execute query
       \q to quit

WARNING:  You are connected to a server with major version 8.3,
but your psql client is major version 8.2.  Some backslash commands,
such as \d, might not work properly.

dbname=> \o /home/yongmo/data25000.csv
dbname=> select url from urltable where scoreid=1 limit 25000;
dbname=> \q