Summary: in this tutorial, we will show you different ways to connect to PostgreSQL database using an interactive terminal program called psql and pgAdmin GUI application.
When you installed PostgreSQL database server, the PostgreSQL installer also installed some useful tools for working with the database server. You can connect to the PostgreSQL database server by using the psql or pgAdmin tool.
Connect to PostgreSQL database using psql
psql is an interactive terminal program provided by PostgreSQL. You can do a lot with psql tool e.g., execute SQL statements, manage database objects, etc.
The following steps show you how to connect to the PostgreSQL database server by using the psql program:
First launch psql program.
Second, enter all the required information such as server, database, port, username and password. If you press enter, psql will use default values inside the square brackets.
Third, you can interact with PostgreSQL database server by using various SQL statements. You can try the following statement to test it out:
1 | SELECT version(); |
Please do not forget to put the semicolon (;) at the end of the statement. After pressing enter, psql will give you the current PostgreSQL version that you have in the system.
Connect to PostgreSQL database using pgAdmin GUI application
The second way to connect to a database is using pgAdmin GUI application. By using pgAdmin GUI application, you can interact with PostgreSQL database server via an intuitive user interface.
The following illustrates how to connect to database using pgAdmin GUI application:
First, launch the pgAdmin application.
Second, double click the PostgreSQL 9.2 under the Servers item. pgAdmin will ask you for the password. You have to provide the password for the postgres
user. Once finish, click OK button to log in to the PostgreSQL server.
Third, choose postgres database and click Execute Arbitrary SQL queries tool from the pgAdmin’s toolbar.
Fourth, enter the following statement:
Fifth, click the execute query button in the toolbar, pgAdmin will display the result in the output panel.
Connect to PostgreSQL database from other applications
Any application that supports ODBC or JDBC can connect to PostgreSQL database server. In addition, if you develop an application that uses an appropriate driver, the application can connect to the PostgreSQL database server as well.
In this tutorial, you’ve learned how to connect to PostgreSQL database server by using different tools including psql and pgAdmin GUI application. Let’s explore the PostgreSQL database objects and find out how can we use them in our applications.