92

PostgreSql バージョンを使用しています:

postgres=# select version();
                           version
-------------------------------------------------------------
 PostgreSQL 9.2.4, compiled by Visual C++ build 1600, 64-bit
(1 row)

postgres=#から...までデータベースに接続していましたが、newdb=#今はnewdb=#データベースにいるので、切断してデータベースに戻りたいpostgres=#です ....

これを行う方法 ?

私は試してみましたdisconnect newdb;

しかし、次のようにエラーを与えます::

postgres=# create database newdb;
CREATE DATABASE
postgres=# \c newdb;
WARNING: Console code page (437) differs from Windows code page (1252)
         8-bit characters might not work correctly. See psql reference
         page "Notes for Windows users" for details.
You are now connected to database "newdb" as user "postgres".
newdb=# disconnect newdb;
ERROR:  syntax error at or near "disconnect"
LINE 1: disconnect newdb;
        ^
newdb=#

これを行う他の方法はありますか、それとも私は何か間違っていますか!!

4

2 に答える 2

107

簡単です。例を見てください。

-- 私のデータベース

postgres=# \l
                               List of databases
   Name    |  Owner   | Encoding | Collate | Ctype |     Access privileges     
-----------+----------+----------+---------+-------+---------------------------
 francs    | postgres | UTF8     | C       | C     | =Tc/postgres             +
           |          |          |         |       | postgres=CTc/postgres    +
           |          |          |         |       | francs=C*T*c*/postgres   +
           |          |          |         |       | select_only=c/francs
 postgres  | postgres | UTF8     | C       | C     | 
 source_db | postgres | UTF8     | C       | C     | =Tc/postgres             +
           |          |          |         |       | postgres=CTc/postgres    +
           |          |          |         |       | source_db=C*T*c*/postgres
 template0 | postgres | UTF8     | C       | C     | =c/postgres              +
           |          |          |         |       | postgres=CTc/postgres
 template1 | postgres | UTF8     | C       | C     | =c/postgres              +
           |          |          |         |       | postgres=CTc/postgres
(5 rows)

--ロール francs として db francs に切り替えます

postgres=# \c francs francs
You are now connected to database "francs" as user "francs".

--ロール postgres として db postgres に切り替えます

francs=> \c postgres postgres

You are now connected to database "postgres" as user "postgres".
postgres=# 

--データベースから切断します

postgres=# \q
于 2013-07-31T07:48:18.723 に答える
47

psql には「切断」はありません。newdb データベースから切断する代わりに、デフォルトの postgres データベースに接続します。

新しいデータベースを作成して接続します。

postgres=# create database newdb;
CREATE DATABASE    
postgres=# \c newdb
You are now connected to database "newdb" as user "postgres".
newdb=#

newdb の接続数を一覧表示します。

newdb=# select datname,numbackends from  pg_stat_database where datname='newdb';
 datname | numbackends
---------+-------------
 newdb   |           1

ここで、切断する代わりに、デフォルトの postgres データベースに接続するだけです。

newdb=# \c postgres
You are now connected to database "postgres" as user "postgres".
postgres=#

現在、newdb には接続がありません。

postgres=# select datname,numbackends from  pg_stat_database where datname='newdb';
 datname | numbackends
---------+-------------
 newdb   |           0
于 2015-12-22T20:26:36.560 に答える