PostgreSQL - SELECT Database

We have already created a database in our previous chapter. You can select the database using either of the following methods −

  • Database SQL Prompt
  • OS Command Prompt
Database SQL Prompt

Assume you have already launched your PostgreSQL client and you have landed at the following SQL prompt −

postgres=#

You can check the available database list using \l, i.e., backslash el command as follows −

postgres-# \l
                             List of databases
   Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges   
-----------+----------+----------+---------+-------+-----------------------
 postgres  | postgres | UTF8     | C       | C     | 
 template0 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
 template1 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
 testdb    | postgres | UTF8     | C       | C     | 
(4 rows)

postgres-# 

Now, type the following command to connect/select a desired database; here, we will connect to the testdb database.

postgres=# \c testdb;
psql (9.2.4)
Type "help" for help.
You are now connected to database "testdb" as user "postgres".
testdb=# 


OS Command Prompt

You can select your database from the command prompt itself at the time when you login to your database. Following is a simple example −

psql -h localhost -p 5432 -U postgress testdb
Password for user postgress: ****
psql (9.2.4)
Type "help" for help.
You are now connected to database "testdb" as user "postgres".
testdb=# 

You are now logged into PostgreSQL testdb and ready to execute your commands inside testdb. To exit from the database, you can use the command \q.

Comments

Popular posts from this blog

Java : Variables Declaring

SQL Self JOIN