How to connect to PostgreSQL using psql

This article describes how to connect to a PostgreSQL database from the command line using the psql program. You can use the psql program as a quick and easy way to access your databases directly. psql is a PostgreSQL interactive terminal that allows you to run PostgreSQL queries and examine the results. psql also has a number of meta-commands and shell-like capabilities that make building scripts and automating a range of activities easier. Please refer to this link to learn more about these commands.

Connecting to PostgreSQL using psql

To connect to PostgreSQL from the command line, follow these steps:

  1. Log in to your A2 Hosting account using SSH.
  2. At the command line, type the following command. Replace dbname with the name of the database, and username with the database username:
    psql dbname username
  3. At the Password prompt, type the database user's password. When you type the correct password, the psql prompt appears.
  4. After you access a PostgreSQL database, you can run SQL queries and more. Here are some common psql commands:

    • To view help for psql commands, type \?.
    • To view help for SQL commands, type \h.
    • To view information about the current database connection, type \conninfo.
    • To list the database's tables and their respective owners, type \dt.
    • To list all of the tables, views, and sequences in the database, type \z.
    • To exit the psql program, type \q.

More Information

Did you find this article helpful? Then you'll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.

We use cookies to personalize the website for you and to analyze the use of our website. You consent to this by clicking on "I consent" or by continuing your use of this website. Further information about cookies can be found in our Privacy Policy.