Basics of Exploring PostgreSQL
Goals and Audience
This post expands on the introduction to PostgreSQL and introduces some helpful commands for exploring a PostgreSQL database.
In the introduction, you created a database called business and accessed the PSQL command prompt by running psql business. This post will reference the same database. If you did not create it already, you can either:
- Run
createdb businessfrom your command line / terminal. - Run
psqlto enter the PSQL command prompt, then enter the commandcreate database business;.
Helpful Commands
Commands in PostgreSQL begin with the backslash character \. While there are many commands available, the following will help you explore your database.
General Commands
\?shows all possible commands.\qquits the PSQL command prompt.
Informational Commands
-
\dnshows you a list of schemas, which are like groups or namespaces for tables. By default, everything goes in thepublicschema. Some use cases require multiple schemas, and for now it’s okay to keep everything inpublic.-
business=# \dn List of schemas Name | Owner --------+------- public | eric (1 row)
-
-
\dtshows you a list of tables. You can also try\dt+if you want to see more detailed information. The output saysList of relations, and in general a relation is another name for a table. There are other types of relations, but most of the time they are tables.-
business=# \dt List of relations Schema | Name | Type | Owner --------+-----------+-------+------- public | customers | table | eric public | purchases | table | eric (2 rows) - The
schemais the schema in which the table was created. For now, it will always bepublic. - The
nameis the name you provide when creating a table. - The
typeis the type of relation. In general this will be atable. - The
owneris the user that created the table.
-
-
\lshows available databases. I refer to PostgreSQL as a database, but technically it is a relational database management system. The system can contain multiple databases, but for now you will use thebusinessdatabase.-
business=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+-------+----------+---------+-------+------------------- business | eric | UTF8 | C | C | postgres | eric | UTF8 | C | C | template0 | eric | UTF8 | C | C | =c/eric + | | | | | eric=CTc/eric template1 | eric | UTF8 | C | C | =c/eric + | | | | | eric=CTc/eric (4 rows) -
You created the
businessdatabase, and the other three are defaults that are created when PostgreSQL is installed. -
Use
\cto connect to a different database. For example, run\c postgresand then run\c businessto switch back. -
Use
\conninfoto see which database you’re currently using:-
business=# \conninfo You are connected to database "business" as user "eric" via socket in "/tmp" at port "5432".
-
-
Formatting Commands
The following commands can help make PostgreSQL output easier to read.
-
\xformats row output vertically (called expanded output). It can be toggled on and off by entering the command repeatedly, or explicitly by entering\x onor\x off. You can also use\x autoif you want the database to decide. This setting isoffby default. The following example shows the difference between when expanded output is off and when it is on.-
business=# select * from purchases; customer_name | amount | purchase_date ---------------+--------+--------------- Eric | 55.01 | 2020-04-20 Eric | 86.57 | 2020-04-21 Melanie | 57.50 | 2020-04-23 (3 rows) business=# \x Expanded display is on. business=# select * from purchases; -[ RECORD 1 ]-+----------- customer_name | Eric amount | 55.01 purchase_date | 2020-04-20 -[ RECORD 2 ]-+----------- customer_name | Eric amount | 86.57 purchase_date | 2020-04-21 -[ RECORD 3 ]-+----------- customer_name | Melanie amount | 57.50 purchase_date | 2020-04-23 business=# \x Expanded display is off.
-
-
\timingshows how long it takes a query to run, which can help you debug performance issues. Like\x, you can toggle it on and off or set it explicitly using\timing onor\timing off. This example shows a query that takes 0.386 milliseconds to execute.-
business=# \timing Timing is on. business=# select * from purchases; customer_name | amount | purchase_date ---------------+--------+--------------- Eric | 55.01 | 2020-04-20 Eric | 86.57 | 2020-04-21 Melanie | 57.50 | 2020-04-23 (3 rows) Time: 0.386 ms business=# \timing Timing is off. business=#
-
Summary
In this guide, you learned:
\?shows all possible commands.\qquits the PSQL command prompt.\dnshows a list of schemas, which are groupings of tables.\dtshows a list of tables, which are a type of relation. In general, you can think of a relation and table as being the same.\lshows a list of available databases. For now, you just have thebusinessdatabase we created. You also learned that PostgreSQL is a relational database management system.\clets you connect to a different database, such as\c postgres(to use the defaultpostgresdatabase), or\c businessto switch back to yourbusinessdatabase.\conninfoshows the database you are currently using.
\xswitches to expanded output mode, which formats rows vertically. This command is helpful when viewing tables with many columns.\timingshows you how long a query takes to run, which is helpful for debugging performance issues. Although you won’t see performance issues yet, future posts will cover possible issues and how to debug them.