如何断开与数据库的连接并返回到 PostgreSQL 中的默认数据库?

我使用的是 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=#

它不工作,有没有其他的方法做到这一点,或者我在任何事情错了! !

130314 次浏览

It's easy, just look the example.

--my databases

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)

--switch to db francs as role francs

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

--swith to db postgres as role postgres

francs=> \c postgres postgres


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

--disconnect from db

postgres=# \q

There is not a 'disconnect' in psql. Instead of disconnecting from your newdb database you connect with the default postgres database.

Create the new database and connect to it:

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

List the number of connections on newdb:

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

Now, instead of disconnecting, just connect with the default postgres database.

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

Now there are no connections on newdb:

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