Introduction to PostgreSQL Client
PostgreSQL client is defined as connect to the database server using command, utility or third party tool. Basically we are using psql utility to connect the database server from OS interface, also we are using pg_admin tool to interact with database server using client interface. We can connect the database server using username, password and hostname of database server, we can also connect the database server using client applications like pg_admin. We can connect to database server from client by using local host or from the remote host using psql utility or third party tool.
Syntax of PostgreSQL Client
Below is the syntax of PostgreSQL Client:
# psql –U username –W user_password
or
# psql –U username –W user_password –d database_name
or
# psql –U username –W user_password –d database_name –h hostname
or
# psql –U username –W user_password –d database_name –p port_no
Below are the parameter description syntax of PostgreSQL client:
- Psql: This is client utility which was used to connect the database server. We can use this utility with the username, password and hostname. Psql is useful and important utility of client which was used to connect to the database server.
- Username: This is important parameter which was used to connect the database server. Username is used to authenticate with database server.
- Password: This is defined as password parameter to authenticate client user with password. Wrong password is not authenticated with the database. We need to provide password with psql utility to connect the database server.
- Database name: This is defined as database name to connect with specified database. To connect to the database we need to provide connect grant on database to the specified user.
- Host name: If we want to connect from another host then we need to use this parameter in PostgreSQL. If we connect to the local host there is no need to define host name with psql utility. This is the optional parameter which is used with psql utility in PostgreSQL.
- Port no: This is defined as port number is used while connecting with database server through port number. This is optional parameter which was used with psql utility in PostgreSQL.
How does PostgreSQL Client works?
Given below shows how the PostgreSQL Client works:
- Client is used to connect to the database server.
Below figure shows that client architecture in PostgreSQL.
- PostgreSQL client server architecture is divided basically into two parts i.e. client and server.
- The client is defined as command execution environment like a psql or we have used special driver for the same.
- PostgreSQL client is used libpq libraries to communicate and connect to the database server.
- Libpq is the library which was used with each client applications through postmaster process in PostgreSQL. This library is similar with OCI which was used in ORACLE.
- Client will first request to the database server through the client utility. Request will contain the SQL query.
- After requesting client to the database server, it will check the request from client after checking request it will authenticate with the database server.
- After authentication is successful then it will response to the client with his request.
- We can connect multiple client at the same time to the server to process the request of the client.
- In PostgreSQL there are number of GUI applications which was used to manage the PostgreSQL database.
- This GUI applications is used to manage the SQL queries and view database activities in PostgreSQL.
- We can use socket while connecting through the command line tool. If we want to connect from the application then we are using the port no while connecting to the database server.
- We can also set up the connection with SSH tunnel in PostgreSQL. SSH tunnel is more secure method while connecting to the database server through client.
- We can also direct set up the connection between our local computer and remote computer.
Examples
Given below are the examples mentioned:
Example #1
Connect from client through username and password.
Below example shows that connection from client through username and password. We have connected the database server through psql utility.
Code:
psql -U postgres –W
Output:
Example #2
Connect from client through username, database name and password.
Below example shows that connection from client through username, database name and password. We have connected the database server through psql utility.
Code:
psql -U postgres –W –d postgres
Output:
Example #3
Connect from client through username, database name, hostname and password.
Below example shows that connection from client through username, database name, hostname and password. We have connected the database server through psql utility.
Code:
psql -U postgres –W –d postgres –h 127.0.0.1
Output:
Example #4
Connect from pg_admin tool to the database server.
Below example shows that connect to the database server through the pg_admin tool. We need to provide below connection parameter while connect to the database server from pg_admin tool.
Output:
Example #5
Connect client through username, database name, hostname, port and password.
Below example shows that connection from client through username, database name, hostname, port and password. We have connected the database server through psql utility.
Code:
psql -U postgres –W –d postgres –h 127.0.0.1 –p5432
Output:
Recommended Articles
This is a guide to PostgreSQL Client. Here we discuss how does PostgreSQL client works along with respective query examples. You may also have a look at the following articles to learn more –