How to Query Data From a Specific Table in PostgreSQL

The term “query data” refers to a process of fetching or retrieving data from single or multiple tables. While working with RDMS like PostgreSQL, querying the data from a table is a very common but crucial task that can be done by a “SELECT” statement. The stated command can be utilized for different purposes, such as querying all data of a table, some specific records of a table, fetching some specific columns from a table or selecting various columns from multiple tables.

This article will demonstrate:

- How to Query Table Data Using SQL Shell?
- How to Query Table Data Using pgAdmin?

How to Query Table Data Using SQL Shell?

The “SELECT” query is used with the “*” symbol to query all the data of a particular table of the selected database. To utilize the “SELECT” query in Postgres, follow the provided syntax:

SELECT * FROM tab_name;

The stated command will fetch all records of the specified “tab_name”.

Example 1: How to Query All Data From a Postgres Table?

Open the “SQL Shell” and run the following metadata command to enlist the available tables in the current database:

\dt
img

Now, utilize the SELECT * command along with the table name to fetch all of its content:

SELECT * FROM author_info;

Here in this query, the “SELECT” command is utilized to query the data of the selected table. The “*” represents a wildcard that helps us select all columns of a table. While “FROM” is a keyword used to specify the table from which the data will be fetched or retrieved:

img

The output shows that all data from the specified table has been successfully queried.

Example 2: How to Query Specific Data in Postgres?

To query specific data from a Postgres table, use the “WHERE” clause with the “SELECT” query as follows:

SELECT * FROM 
tab_name 
WHERE condition;

The “WHERE” clause will restrict the SELECT statement to query only those records that meet the specified condition. For example, the below piece of code will query only those authors whose experience is more than five years:

SELECT * FROM author_info
WHERE author_exp > 5;
img

The table records that meet the specified condition have been successfully queried.

How to Query Table Data Using pgAdmin?

The “View/Edit Data” option of the pgAdmin assists us in querying the data of a particular table. It directs us to the Query tool, where we can edit the query to perform specific operations, such as filtering the selected table, adding new rows, updating an existing row, etc.

Example 2: Querying Table Data Using pgAdmin

To query the table data using pgAdmin, first, extend the “Servers” list, select a desired database, and then navigate to “Schemas”:

img

Now search for the “public” section and then navigate to the “Tables” section:

img

Select the table from the available list of the selected database:

img

Right-click on the selected table, and choose the “All Rows” option from the “View/Edit Data” option:

img

Consequently, the selected table will be queried:

img

That was all about querying the data from a specific table in Postgres.

Conclusion

To query data from a Postgres table, open SQL Shell, and execute the “SELECT *” command followed by the table name. While, in the case of pgAdmin, first, expand the “Servers” tree, select a database, select a schema, and then select a table from the available list. After that, right-click on the selected table and choose the “View/Edit Data” option. This article presented various methods to query the table data in PostgreSQL.