[Dev] postgresqlでインポートしてみました

in japanese •  8 months ago  (edited)

データベースに接続してみる

データベース接続
psql -d yasudb

テーブル作成
CREATE TABLE aaa (
a varchar2(100)
);

カラム追加
ALTER TABLE aaa ADD b int;
ALTER TABLE aaa ADD c char(10);

終了
\q

ターミナル画面から、実行してみる

CSVをインポート
psql -d yasudb -c "\copy aaa from stdin DELIMITER ','" <<EEE
111,1,11
222,2,22
EEE

TSVをインポート
psql -d yasudb -c "\copy aaa from stdin DELIMITER e'\t'"<<EEE
111 1 11
222 2 22
EEEその他

テーブルの内容を表示
psql -d yasudb -c "select * from aaa"

その他

コマンド <<EEE
~
EEE
は、最近覚えた方法

from stdin のstdinは標準入力、通常はインポートファイルの絶対パスを指定する。

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!