How to connect Postgres via Terminal?

From PheonixSolutions
Jump to navigation Jump to search

Introduction :

PostgreSQL, commonly known as Postgres, is a powerful open-source relational database management system. It excels in data integrity, extensibility, and compliance with SQL standards. Postgres is widely utilized for handling complex and high-performance database applications. Its active community and continuous development.

Prerequisites :
Host Authentication: Update the pg_hba.conf file on the PostgreSQL server to allow your local machine's IP address or network range. Configure the authentication method (e.g., password authentication).
PostgreSQL Server Configuration: Check the PostgreSQL server configuration (postgresql.conf) to ensure it allows remote connections. Look for the listen_addresses parameter and set it to the appropriate IP address
Method 1 : using terminal

Step 1:

Enter the following command to launch the Postgres command-line client.

psql -h your_host -d your_database -U your_user -W

Replace the above commands with your Credentials.

  • your_host: The host or IP address of your PostgreSQL server.

  • your_database: The name of the PostgreSQL database you want to connect to.

  • your_user: Your PostgreSQL username.

  • -W: This flag prompts for the password.

Step 2 : Enter Password:

Enter password:

If the connection is successful, you should see the PostgreSQL prompt:

psql (12.x)

  • \q: Quit the PostgreSQL shell.

Method 2 : using pgadmin
Install pg admin using this link https://www.pgadmin.org/download/

Note : install the pgadmin version which is applicable for postgres version wh

Step 1: Logging in to the pgAdmin client requires you to set a master password when you launch it for the first time.

Step 2 : We need to provide the connection details for that remote server are in the connection tab below:

Step 3 : Click save. Now you are ready to connect to the server by double-clicking the server name under Servers and entering your password:

After you provide the server credentials, you will be redirected with the dashboard.