1
2

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 1 year has passed since last update.

psqlコマンド一覧

Last updated at Posted at 2023-04-06

psqlコマンド

psqlを実行してpostgreSQLの対話モードに入る

PostgreSQLのデフォルトのルートユーザーは「postgres」です。
このため、ユーザーを指定する「-U」オプションで「postgres」を指定して「psql」コマンドを実行します。

ターミナルからpostgresSQL対話モードに入る
$ psql -U postgres
PostgreSQLを起動した状態で、次のコマンドで接続
$ psql [データベース名] #dbに接続

postgres=>\q #終了
操作
postgresql
$ \l # データベース一覧を表示
$ \d  # テーブル一覧を表示
$ \du # ユーザー一覧を表示
$ \q # psqlのメタコマンド終了
データベース一覧を表示
$ psql -l
DBユーザ操作
ユーザー作成・削除
$ createuser (ユーザー名)
$ dropuser (ユーザー名)
テーブル操作
データベース作成・削除
$ createdb (データベース名)
$ dropdb (データベース名)

postgreSQLの特性

ゼロパディング対応

http://senoway.hatenablog.com/entry/2014/03/18/161308

PHPで123を000123のように6桁の0詰めを作るなら、例えば、
sprintf('%06d',123)のようにできるが、そもそもDBから取得する際にSQL内で、TO_CHAR(123,'FM000000')とすれば 000123が取得できます。

123の部分は適宜カラム名等に置き換えてください。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?