To read from a PostgreSQL table and view its contents, you can use the ‘SELECT’ command as shown below (note that you need to be connected to the right database that contains whichever table you’re reading):
SELECT * FROM tablename;
The command above displays the contents of the entire table in the order it was originally inserted. The ‘*’ means ‘all columns’. You can replace the ‘*’ with one or more column names, separated by commas.
Let’s use the ‘battery’ table we created in the example earlier, and select the model and capacity columns.
SELECT model, capacity FROM batteries;
The code above will display the all batteries entered in the database, but it only shows the ‘model’ and ‘capacity’ columns we selected above.
Get some practice with the insert command so we can try sorting and filtering next. Enter the following commands so we’ll have some data to work with:
INSERT INTO batteries VALUES ('INR18650-25R', 'Lithium-Ion', 9.2); INSERT INTO batteries VALUES ('INR18650-30Q', 'Lithium-Ion', 11.1); INSERT INTO batteries VALUES ('NCR18650GA', 'Lithium-Ion', 12.5); INSERT INTO batteries VALUES ('US18650VTC5D', 'Lithium-Ion', 10.3);