我正在使用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=#
这不起作用,还有其他方法可以做到这一点,或者我在任何事情上都错了!!
很容易,只看示例。
-我的数据库
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)
-切换至法郎作为角色法郎
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
psql中没有'disconnect'。无需断开与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
要连接到其他数据库,请执行\c db_name
或简单的方法是做\q
psql
如果要返回默认数据库,可以执行以下操作:\ c postgres应该相等。