Introduction to Postgres Switch Database
Whenever you are using the database to store the data of your application or program or for some other reason, we often feel the need to create and maintain more than one database. In such cases, while working on Postgres databases and executing various queries, we need to first connect with the database that we wish to use, and then if you have to switch to some other database, the PostgreSQL provides the metacommand \c that can be used to switch from one database to another in PostgreSQL. In this article, we will see about \c metacommand’s syntax and try to use it in one of the examples to understand its usage.
Syntax:
\c targetdb;
Or
\connect targetdb;
Where,
- targetdb: It is the name of the database you wish to connect to while you are connected to some other database.
Metacommands are provided in psql for easy manipulation and working of the database related queries and making the job of database administrators easy by providing the shortcut queries that are compact and easy to use. Metacommands mostly begin with \ and are further appended with a letter of word related to the query task and then are followed by the parameters required for that query execution.\c and \connect are two metacommands provided for database switching purpose. Whenever you are executing \c, or \connect command, your current connection with the current database will be closed, and a new connection with the database named targeted will be established.
Example of Postgres Switch Database
Given below is the example of Postgres Switch Database:
Whenever you log in to the PostgreSQL database server, the default database that you are connected to is the Postgres database that is mostly present by default after installing PostgreSQL in your system. You can even change the default database that you wish to connect to.
Firstly, let us login to Postgres by using the following command.
Code:
sudo su – postgres
And enter the password that you have set.
Output:
Now, you will have to enter into the psql command prompt.
Code:
psql
And then enter the password if prompted. The output will be as follows with a psql command prompt shell where you can run your Postgres queries such as meta-commands.
Output:
By default, you can see that you are connected to the Postgres database. The name of the database you are connected to can be seen from the command prompt shell; for example, in our case, postgres=# represents the same.
Now, we will list all the databases present in our Postgres database server by using the metacommand \l or \list that list out the names of all the databases present in the current database server. Firing \l and \list give the following output on my server.
Code:
\l
Output:
template0, template1, and Postgres are the default databases that are created whenever you install Postgres. We have created one more database named educba to which we want to switch to. You can create a new database if you want to by using the CREATE DATABASE query.
For example, for creating a database named demo, we will fire the following command.
Code:
create database demo;
Output:
Further, by using the \l or \list command, you can verify whether your database is created successfully.
Now, we want to switch to the educba database from the current database Postgres.
For this, we can either use:
Code:
\c educba;
Or
\connect educba;
Output:
A message is displayed saying, you have connected to database “educba” along with the name of the user using which you are connected to, which in our case is Postgres. Also, we can see the command prompt shell changing from postgres=# to educba=# that ensures that we have switched out the database successfully to educba. What happens when you fire \c, or \connect command is that the database connection that was created initially to connect to the Postgres database is closed, and a new database connection with educba is created for the user with which you are log in to Postgres at that particular time.
Let us switch back to Postgres by executing the \c postgres command that gives the following output.
Code:
\c postgres;
Output:
Now, instead of \c, let us execute \connect educba; command and check whether we can switch to the educba database.
Code:
\connect educba;
Output:
That is the same as \c educba; command execution result.
Alternatively, when you want to connect to the educba database, you can do so at the beginning itself while opening psql command prompt shell so that no switching needs to be done afterwards, provided you know the name of the database you want to connect to and that database is present in your current database server.
For that, firstly, enter the following command to login using Postgres user and enter the password.
Code:
sudo su - postgres
Then enter the command.
Code:
psql dbname;
Where dbname is the name of the database, you wish to connect to.
Code:
psql educba;
Output:
As you can see, the command prompt shell opens to educba=#, and hence we are connected to the educba database with a new connection instead of the default database. In this case, while psql tries to create a new database connection, it goes for searching whether the database named dbname is present and then creates a connection with the database. In case if you supply the wrong dbname, then it won’t allow connecting to.
Let us try connecting to the educba1 database that is not present on my database server of Postgres using the psql educba1; command that results in the following output.
Code:
psql educba1;
Saying that database doesn’t exist.
Output:
Conclusion
We can switch between the databases in PostgreSQL using \c or \connect metacommand, which creates a new connection and closes the current one. You can log in and connect to the database you want to other than the default one while opening the psql command shell itself by specifying the name of the database in the parameter of psql command as shown above.
Recommended Articles
This is a guide to Postgres Switch Database. Here we discuss the introduction to Postgres Switch Database along with example respectively. You may also have a look at the following articles to learn more –