• IT助成金利用
  • 無料デモ紹介ZOOM等オンラインでのご相談も可能です
  • お問い合わせ

※ ワンダーロボ設計書および関連する自動開発手法について、すべての著作権はワンダフルフライ株式会社に帰属します。

PostgreSQL利用コマンド(Linux版)

よく使うPostgreSQLで利用可能なコマンドのチートシートです。

端末上で使うコマンド

1.サーバの起動
$ pg_ctl start -D /usr/local/var/postgres

2.サーバの終了

$ pg_ctl stop -D /usr/local/var/postgres

3.サーバが起動しているかの確認
$ ps aux | grep postgres


4.PostgreSqlバージョン表示
$ psql -V or $psql –version

これから、sudoを使用してpostgresユーザーに切り替え、同じユーザーで操作する必要があります。
sudo -i -u postgres

psqlモードに入る
$ psql -U postgres

5.データベース一覧表示
$ psql -l or

psql モードで確認
postgres=# \l  or  postgres=# SELECT datname FROM pg_database;

6.データベース接続
$ psql -d database -U user -h host
  • -d: データベース名(未指定だと、ログインユーザー名のデータベースに接続する)
  • -U: ユーザ名(未指定だと、ログインユーザー名になる)
  • -h: ホスト名(未指定だと、localhostになる)


7.他のデータベースに接続
postgres=# \c dbname

8.データベース作成
postgres=# create database dbname;

9.接続中のデータベースの情報を表示
postgres=# \conninfo

10.テーブル一覧を表示
postgres=# \z or postgres=# SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname = ‘public’;

11.テーブル定義を確認
postgres=# \d tablename

12.カレントディレクトリ変更
postgres=# \cd directory

13.CSV形式のファイルをテーブルに挿入
postgres=# \copy tablename from filename DELIMITER AS ‘,’

14.DUMP、SQLファイルデータのインポート
$ pg_restore -U postgres -d databasename data.dump or $ pg_restore -U postgres -d databasename data.sql

15.DUMPデータのバックアップ
$ pg_dump -U username  databasename > /path/to/backup/file.sql