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)

--切换到 db francs 作为角色 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=# 

--断开与数据库的连接

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 回答