You can use the psql utility to interact with your PostgreSQL database directly from the terminal. With psql similar to the mysql you can run queries, manage databases and automate tasks with scripts over the command-line.
sudo -u postgres psql template1
Once in psql, check your connection to make sure you are in the right database by running:
\conninfo
If you need to switch to a different database before running queries, use:
\c database_name
Once in the correct database, you can start querying data. To see what tables exist before running a query, use:
\dt
To see the structure of a table before retrieving its data, use:
\d table_name
Then you can retrieve all rows from a table with:
SELECT * FROM table_name;
For help with SQL commands in psql type:
\h
To exit PostgreSQL you will need to enter the command shown below.
\q