0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

大量のSQLをまとめて実行する方法

0
Posted at

課題

大量のsqlファイルが保存されてあるディレクトリがある。
一つ一つ実行するのは面倒くさい。
これらをまとめて実行する。

TL;DR psql version

exec-sql.sh
#!/bin/bash

## EDIT HERE
DIR_NAME=./sql_files

find $DIR_NAME -type f -name '*.sql' | sort | while read -r f; do
  echo "==> $f"
  PGPASSWORD='hogehoge'  psql -h localhost -p 5432 -U test_user -d test_db --set=ON_ERROR_STOP=on -f "$f" || exit 1
done
  • ポイント
    • -h, -p オプションでホストとポート番号を指定してあげる。
    • 環境変数PGPASSWORDをセットすることで、毎回パスワードを聞かれない。

実行方法

$ chmod +x exec-sql.sh
$ ./exec-sql.sh
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?