Book Image

PostgreSQL Administration Essentials

Book Image

PostgreSQL Administration Essentials

Overview of this book

Table of Contents (14 chapters)

Understanding the existing databases


Once the database has been launched, we can connect to PostgreSQL using a psql frontend:

psql postgres

Ideally, you connect to a database called postgres, which can be found in any database instance. Some systems don't encourage people to log in as a postgres user. Therefore, you might want to use sudo here as well to log in to PostgreSQL. If this works for you, you can make PostgreSQL display a list of existing databases, where \l will do the job:

postgres=# \x
Expanded display is on.
postgres=# \l
List of databases
-[ RECORD 1 ]-----+----------------------
Name              | postgres
Owner             | postgres
Encoding          | UTF8
Collate           | en_US.UTF-8
Ctype             | en_US.UTF-8
Access privileges |
-[ RECORD 2 ]-----+----------------------
Name              | template0
Owner             | postgres
Encoding          | UTF8
Collate           | en_US.UTF-8
Ctype             | en_US.UTF-8
Access privileges | =c/postgres
                  | postgres=CTc/postgres
-[ RECORD 3 ]-----+----------------------
Name              | template1
Owner             | postgres
Encoding          | UTF8
Collate           | en_US.UTF-8
Ctype             | en_US.UTF-8
Access privileges | =c/postgres
                  | postgres=CTc/postgres

Congratulations, you've completed your first task using the psql shell. If you executed \l as proposed, you might have seen that the table is too wide to be displayed properly. To avoid this, you can use \x to transpose the output and display each column as a separate line. This little feature can come in handy whenever you have to read a wide table.

Tip

If you want to customize the psql shell for your needs, you might want to consider writing a .psqlrc file. It can automatically set things such as \x for you on every login.

In an empty database instance, you will already find the three existing databases template0, template1, and postgres. The rule for you as an end user is simple: always connect to the postgres database and try to avoid connections to the template databases (template0 does not allow connections anyway); these databases are only here to act as a role model in case you create a new database. Make sure that no useless objects are in template0 or template1 because whenever you create an additional database, these useless objects are cloned.