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

后端 未结 2 1681
长发绾君心
长发绾君心 2021-01-31 06:50

I\'m using PostgreSql version :

postgres=# select version();
                           version
-------------------------------------------------------------
 P         


        
相关标签:
2条回答
  • 2021-01-31 07:20

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

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

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

    Now there are no connections on newdb:

    postgres=# select datname,numbackends from  pg_stat_database where datname='newdb';
     datname | numbackends
    ---------+-------------
     newdb   |           0
    
    0 讨论(0)
  • 2021-01-31 07:38

    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

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

    --swith to db postgres as role postgres

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

    --disconnect from db

    postgres=# \q
    
    0 讨论(0)
提交回复
热议问题