テーブルデータをファイル/csvにエクスポートする方法 – PostgreSQL
PostgreSQLには、PostgreSQLデータベースからデータをエクスポートするための使いやすいエクスポートツールが付属しています。このチュートリアルでは、PostgreSQLからファイルまたはCSVファイルにデータをエクスポートする方法を説明します。
1. PostgreSQLを接続します.
psqlコマンドを使用して、PostgreSQLデータベースに接続します。
$ psql -p 5433 -U dba dbname
P.S 5433は自分のPostgreSQLポート番号です.
2.エクスポート準備
“` \ o/home/yongmo/data25000.csv` “と入力すると、PostgreSQLは次のクエリ結果をファイル”/home/yongmo/data25000.csv “にエクスポートするよう指示します。
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