26
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Postgres: CLIからのSQL実行はpsql -cを使う

Posted at

コマンドラインから直接にPostgreSQLのSQLを実行したいときはpsqlコマンドを使う。-cもしくは--commandオプションに実行したいSQLを渡す:

psql データベース名 -c "SQL"

たとえば

psql myapp -c "SELECT * FROM foo"

認証が必要な場合は、-U--usernameオプションにユーザ名を、環境変数PGPASSWORD1にパスワードをセットして実行する。

例:

PGPASSWORD=passwd psql myapp -U alice -c "SELECT * FROM foo"
  1. 環境変数PGPASSWORDはセキュリティの観点から非推奨なので、頻繁に認証する場合はパスワードを安全に管理するためにパスワードファイルを使うようにしよう。

26
16
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
26
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?