2013-07-31 13 views
37

Sto utilizzando la versione PostgreSQL:Come disconnettersi da un database e tornare al database predefinito in PostgreSQL?

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

avevo collegato ad un database postgres=#-newdb=# .... Ora sono in newdb=# Database voglio scollegarlo e tornare a postgres=# database. ...

Come fare questo?

ho provato con disconnect newdb;

ma la sua dando erroe come ::

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

si mangia di lavoro c'è qualche altro modo per fare questo o mi sbaglio in qualcosa !!

+0

So che sembra un po 'ovvio, ma hanno è stata selezionata la [ 'documentazione psql'] (http://www.postgresql.org /docs/current/static/app-psql.html)? Un suggerimento: non esiste un comando SQL 'disconnect' o' psql'. –

+0

Non esiste un "database predefinito" in Postgres. Devi collegarti esplicitamente a un database usando '\ c' –

+0

È quello che succede non possiamo usare [DISCONNECT documentation] (http://www.postgresql.org/docs/9.1/static/ecpg-sql-disconnect.html) per uscire dalla connessione @a_horse_with_no_name – 09Q71AO534

risposta

50

È facile, basta guardare l'esempio.

--Il mio database

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) 

- interruttore per franchi db come franchi ruolo

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

- swith a DB postgres Postgres ruolo

francs=> \c postgres postgres 

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

- disconnettersi dal db

postgres=# \q 
+3

è giusto, ma c'è un altro modo come usare DISCONNECT in SQL [Documentation] (http://www.postgresql.org/docs/9.1/static/ecpg-sql-disconnect.html) @ franchi – 09Q71AO534

+1

@ user2561626: potrebbe essere no, I non sono sicuro – francs

+0

\ q si chiuderà la sessione e se voglio connettermi ad un altro DB allora dovrei \ q e poi \ c di nuovo. Quindi è uscito dal database, senza disconnessione da una sessione attiva. – Ayush

18

Non c'è un 'disconnessione' in psql. Invece di disconnetterti dal tuo database newdb ti connetti con il database postgres predefinito.

creare il nuovo database e connettersi ad esso:

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

Lista il numero di connessioni su newdb:

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

Ora, invece di scollegare, basta collegare con il database Postgres default.

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

Ora non ci sono connessioni su newdb:

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

+1 per "Non c'è una" disconnessione "in psql Invece di disconnettersi dal database newdb ci si connette con il database postgres predefinito.". Per me non era così ovvio (newbie Postgres) –

Problemi correlati