Postgresql-select-database

提供:Dev Guides
移動先:案内検索

PostgreSQL-SELECTデータベース

この章では、データベースにアクセスするさまざまな方法について説明します。 前の章ですでにデータベースを作成していると仮定します。 次の方法のいずれかを使用してデータベースを選択できます-

  • データベースSQLプロンプト
  • OSコマンドプロンプト

データベースSQLプロンプト

PostgreSQLクライアントをすでに起動しており、次のSQLプロンプトに到達したとします-

postgres=#

次のように、バックスラッシュelコマンドを使用して、 \ l を使用して、使用可能なデータベースリストを確認できます-

postgres-# \l
                             List of databases
   Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges
-----------+----------+----------+---------+-------+-----------------------
 postgres  | postgres | UTF8     | C       | C     |
 template0 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
 template1 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
 testdb    | postgres | UTF8     | C       | C     |
(4 rows)

postgres-#

次に、次のコマンドを入力して、目的のデータベースに接続/選択します。ここでは、_testdb_データベースに接続します。

postgres=# \c testdb;
psql (9.2.4)
Type "help" for help.
You are now connected to database "testdb" as user "postgres".
testdb=#

OSコマンドプロンプト

データベースにログインするときに、コマンドプロンプトからデータベースを選択できます。 以下は簡単な例です-

psql -h localhost -p 5432 -U postgress testdb
Password for user postgress: ****
psql (9.2.4)
Type "help" for help.
You are now connected to database "testdb" as user "postgres".
testdb=#

これでPostgreSQL testdbにログインし、testdb内でコマンドを実行する準備ができました。 データベースを終了するには、コマンド\ qを使用できます。