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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17963348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 00:19:40  来源:igfitidea点击:

How to Disconnect from a database and go back to the default database in PostgreSQL?

postgresqlpostgresql-9.1postgresql-9.2

提问by 09Q71AO534

I'm using PostgreSql version :

我正在使用 PostgreSql 版本:

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

i had connected to a database from postgres=#to newdb=#.... Now i'm in newdb=#Database i want to disconnect it and go back to postgres=#database ....

我从连接到数据库postgres=#newdb=#....现在我在newdb=#数据库中,我想拔掉,然后返回到postgres=#数据库....

How to do this ?

这该怎么做 ?

I have tried with disconnect newdb;

我试过 disconnect newdb;

but its giving erroe as::

但它给出的错误是:

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

it isnt working is there any other way to do this or am i wrong in anything!!

它不起作用有没有其他方法可以做到这一点,或者我有什么问题!!

回答by francs

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

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

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

--swith to db postgres as role postgres

--切换到 db postgres 作为角色 postgres

francs=> \c postgres postgres

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

--disconnect from db

--与数据库断开连接

postgres=# \q

回答by Hawkez

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

psql 中没有“断开连接”。与默认的 postgres 数据库连接,而不是与 newdb 数据库断开连接。

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 上的连接数:

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.

现在,无需断开连接,只需连接默认的 postgres 数据库即可。

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

Now there are no connections on newdb:

现在newdb上没有连接:

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