Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm using PostgreSql version :

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

How to do this ?

I have tried with 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!!

like image 892
09Q71AO534 Avatar asked Jul 31 '13 06:07

09Q71AO534


People also ask

How do I switch between databases in PostgreSQL?

Postgres has a different way to switch databases, you do so using one of its meta-commands. Once you are in the Postgres terminal, you enter using the psql command, you can switch databases with \connect <db_name> command or with the shorter \c <db_name> .

How do I exit a database in PostgreSQL?

The meta-command for exiting psql is \q .

How do I terminate a connection in PostgreSQL?

You can use pg_terminate_backend() to kill a connection. You have to be superuser to use this function. This works on all operating systems the same.

How do I disconnect from database?

To disconnect from a database, click the connection in the Database Navigator or Projects view, and then click the Disconnect button in the toolbar or click Database -> Disconnect on the main menu: You can also right-click the connection and click Disconnect on the context menu.


1 Answers

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 
like image 59
francs Avatar answered Sep 28 '22 09:09

francs