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

时间:2013-07-31 06:40:56

标签: postgresql postgresql-9.1 postgresql-9.2

我正在使用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;

但它给了erroe :::

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=#

它没有工作是否还有其他方法可以做到这一点,或者我错了!

2 个答案:

答案 0 :(得分:75)

这很容易,只看例子。

- 我的数据库

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)

- 切换到db francs作为角色法郎

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

- 转换为db postgres作为角色postgres

francs=> \c postgres postgres

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

- 与db断开连接

postgres=# \q

答案 1 :(得分:32)

psql中没有'断开'。您可以使用默认的postgres数据库连接而不是从newdb数据库断开连接。

创建新数据库并连接到它:

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