How to use psql PostgreSQL database lists and tables

When managing PostgreSQL database server, one of the most common tasks you might want to do is list the database and its tables.

PostgreSQL comes with an interactive tool called psql, which allows you to connect to the server and run their queries. In use psql, it can also use the meta-command. These commands are useful for scripting and command-line administration. All meta commands begin with a non-quoted backslash, also known as the backslash command.

This tutorial explains how to use psql to display database and table in PostgreSQL server.

List Database

You can use the psql command to connect to PostgreSQL server to any system user. The server configuration, a user may need to enter a password to connect to its terminal psql. To access the psql terminal as a user you are currently logged in, you can just type psql.

After installing PostgreSQL package will create an administrative user named "postgres" is. By default, this user can connect to a local PostgreSQL server without a password.

To "postgres" user identity access terminal psql, run:

sudo -u postgres psql

The sudo command allows you to run commands as another user.

psql terminal execution from \ l or \ list meta-command lists all databases:

\l

Output will include a number of databases, each database name, owner, encoding and access:

Output is as follows:

                              List of databases
   Name    |  Owner   | Encoding | Collate |  Ctype  |   Access privileges   
-----------+----------+----------+---------+---------+-----------------------
 odoo      | odoo     | UTF8     | C       | C.UTF-8 | 
 postgres  | postgres | UTF8     | C.UTF-8 | C.UTF-8 | 
 template0 | postgres | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |          |          |         |         | postgres=CTc/postgres
 template1 | postgres | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |          |          |         |         | postgres=CTc/postgres
(4 rows)

PostgreSQL database server has three default created, template0, template1 and postgres. The first two are templates to use when creating a new database.

If you want to get information about database size, and description information about the default table space, use the \ l + or \ list +. Only when the current user can connect to the database, the database size appears.

To obtain a list of databases without access psql shell, use the -c switch shown below:

sudo -u postgres psql -c "\l"

Another method is to use the database lists the following SQL statement:

SELECT datname FROM pg_database;

And \ l meta-command different from the above query will show only the name of the database:

  datname  
-----------
 postgres
 odoo
 template1
 template0
(4 rows)

The following table lists

To list all the tables in a specific database first, you need to use the \ c or \ connect meta-command to connect to it. You log in psql terminal user must be able to connect to the database.

For example, to connect to a database named "odoo", you would type:

\ C odoo

After switching database, using \ dt list all database tables:

Output will include a number of tables, each table name and its architecture, and the type of owner:

                              List of relations
 Schema |                        Name                         | Type  | Owner 
--------+-----------------------------------------------------+-------+-------
 public | base_import_import                                  | table | odoo
 public | base_import_mapping                                 | table | odoo
 public | base_import_tests_models_char                       | table | odoo
 ...
 public | web_editor_converter_test_sub                       | table | odoo
 public | web_tour_tour                                       | table | odoo
 public | wizard_ir_model_menu_create                         | table | odoo
(107 rows)

If the database is empty, then the output would be as follows:

No relations found.

To obtain information about the size of the table, use the instructions \ dt +.

in conclusion

You've learned how to use the psql command to list PostgreSQL databases and tables.

Guess you like

Origin www.linuxidc.com/Linux/2019-08/159896.htm