Select Database PostgreSQL

Posted on

How do you choose a database to be accessed in PostgreSQL if you don’t specify the database name in the connection parameter of command when connecting to PostgreSQL database server ?.

The answer is quite easy and it just needs several characters to be typed as soon as we have already connected to PostgreSQL databas server.

Using GUI tools and utilities might be the easy way to just select the database we want to access. But not to forget that by using GUI tools and utilities it will affect the database which are being displayed and it depends on the user whom is used to connect to the PostgreSQL database server.

Supposed if we connect with user ‘postgres’, by the default since it is a default user it might have access to all databases. On the contrary, if we create a new user, we have to specify permission to that new user whether it can be used to connect to access a specific¬†database.

1. First of all, we have to connect to PostgreSQL command console as follows  :

user@hostname:~$ psql -Upostgres
Password for user postgres:
psql (9.5.3, server 9.3.10)
Type "help" for help.


2. After successfully connected to PostgreSQL command console if the database name isn’t specified just type the following command to be able to select the database to be accessed for further operation :

postgres=#\c database_name

\c : The command which is used to connect to a certain database, it is followed with the name of the database which is going to be connected. We can associate it or we can image it that c is the abbreviation of 'connect' to make it easy to be remembered.
database_name : The name of the database which is used to be accessed or to be connected

Below is the execution of the above command in PostgreSQL command console :

postgres=# \c testing;
psql (9.5.3, server 9.3.10)
You are now connected to database "testing" as user "postgres".

As shown in the above output, we are already connected to database named ‘testing’ as user ‘postgres’.

One thought on “Select Database PostgreSQL

Leave a Reply