1
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?

More than 5 years have passed since last update.

postgresql:ログインせずに中身を削除してインポートするコマンド備忘録

Last updated at Posted at 2017-12-11

たまに使うけど絶妙な頻度なので忘れてる

コマンド

psql -U ユーザー名 -d DB名 -c "drop schema public cascade" # 中身削除
psql -U ユーザー名 -d DB名 -c "create schema public" # schema作成
psql -U ユーザー名 -d DB名 < ./tmp/dump_20170101.sql # sqlファイルインポート

オプション

オプション名 説明
-U ユーザー名を引数で指定する
-d DB名を引数で指定する
-c 実行したいsqlを引数で指定する。ダブルクォーテーションで囲む。セミコロンは入れておいた方が無難かも。今回の肝。

sql

sql 説明
drop schema public cascade publicという名前のschemaを削除し、その際にcascadeオプションを指定することでpublic schemaに含まれるオブジェクト(テーブル、関数など)も削除する
create schema public publicという名前のschemaを作成する

用語

用語 説明
schema dbを格納する場所。一つのschema内に複数のdbを作成することが可能。階層みたいなもの。
public schema名。publicと名付けられたschemaはどのユーザーからでも操作できるschemaになる。多分予約されていてそういう機能を実現できているんだろうけど自信無い。。
cascade dropコマンドのオプション。schemaに含まれるオブジェクト(テーブル、関数など)も削除する

まとめ

普段はpublic schemaしか触らないため気にする機会が無かったので調べてみました。

  • -c オプションでログインせずに操作できる
1
0
2

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
1
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?