Здесь показаны различия между двумя версиями данной страницы.
| Следующая версия | Предыдущая версия | ||
|
postgres:show_tables [2021/11/04 16:24] werwolf создано |
postgres:show_tables [2023/01/12 12:18] (текущий) |
||
|---|---|---|---|
| Строка 1: | Строка 1: | ||
| ====== PostgreSQL Show Tables ====== | ====== PostgreSQL Show Tables ====== | ||
| - | If this PostgreSQL Tutorial saves you hours of work, please whitelist it in your ad blocker рџ and | ||
| - | [[https://www.postgresqltutorial.com/donation/|Donate Now]] | + | Сначала подключитесь к PostgreSQL с помощью инструмента psql. |
| - | to help us вќ¤пёЏ pay for the web hosting fee and CDN to keep the website running. | + | <code bash> |
| - | + | $ psql -U postgres -W | |
| - | **Summary**: in this tutorial, you will learn how to show tables in PostgreSQL using ''psql'' tool and ''pg_catalog'' schema. | + | |
| - | + | ||
| - | If you are coming from MySQL, you may want to use the popular ''[[http://www.mysqltutorial.org/mysql-show-tables/|SHOW TABLES]]'' statement that displays all tables in a specific database. | + | |
| - | + | ||
| - | PostgreSQL does not support the ''SHOW TABLES'' statement directly but provides you with an alternative. | + | |
| - | + | ||
| - | ===== PostgreSQL show tables using psql ===== | + | |
| - | + | ||
| - | First, connect to PostgreSQL using psql tool. | + | |
| - | + | ||
| - | <code> | + | |
| </code> | </code> | ||
| - | .wp-block-code { | + | ''-U''флаг означает **u**ser and ''-W'' опция требует, чтобы вы предоставили пароль. В этой команде вы используете пользователя postgres для входа на сервер базы данных PostgreSQL. |
| - | border: 0; | + | |
| - | padding: 0; | + | |
| - | } | + | |
| - | .wp-block-code > div { | + | Во-вторых, введите пароль для пользователя postgres и нажмите на клавиатуре Enter: |
| - | overflow: auto; | + | <code bash> |
| - | } | + | Password for user postgres: postgres=# |
| - | + | ||
| - | .shcb-language { | + | |
| - | border: 0; | + | |
| - | clip: rect(1px, 1px, 1px, 1px); | + | |
| - | -webkit-clip-path: inset(50%); | + | |
| - | clip-path: inset(50%); | + | |
| - | height: 1px; | + | |
| - | margin: -1px; | + | |
| - | overflow: hidden; | + | |
| - | padding: 0; | + | |
| - | position: absolute; | + | |
| - | width: 1px; | + | |
| - | word-wrap: normal; | + | |
| - | word-break: normal; | + | |
| - | } | + | |
| - | + | ||
| - | .hljs { | + | |
| - | box-sizing: border-box; | + | |
| - | } | + | |
| - | + | ||
| - | .hljs.shcb-code-table { | + | |
| - | display: table; | + | |
| - | width: 100%; | + | |
| - | } | + | |
| - | + | ||
| - | .hljs.shcb-code-table > .shcb-loc { | + | |
| - | color: inherit; | + | |
| - | display: table-row; | + | |
| - | width: 100%; | + | |
| - | } | + | |
| - | + | ||
| - | .hljs.shcb-code-table .shcb-loc > span { | + | |
| - | display: table-cell; | + | |
| - | } | + | |
| - | + | ||
| - | .wp-block-code code.hljs:not(.shcb-wrap-lines) { | + | |
| - | white-space: pre; | + | |
| - | } | + | |
| - | + | ||
| - | .wp-block-code code.hljs.shcb-wrap-lines { | + | |
| - | white-space: pre-wrap; | + | |
| - | } | + | |
| - | + | ||
| - | .hljs.shcb-line-numbers { | + | |
| - | border-spacing: 0; | + | |
| - | counter-reset: line; | + | |
| - | } | + | |
| - | + | ||
| - | .hljs.shcb-line-numbers > .shcb-loc { | + | |
| - | counter-increment: line; | + | |
| - | } | + | |
| - | + | ||
| - | .hljs.shcb-line-numbers .shcb-loc > span { | + | |
| - | padding-left: 0.75em; | + | |
| - | } | + | |
| - | + | ||
| - | .hljs.shcb-line-numbers .shcb-loc::before { | + | |
| - | border-right: 1px solid #ddd; | + | |
| - | content: counter(line); | + | |
| - | display: table-cell; | + | |
| - | padding: 0 0.75em; | + | |
| - | text-align: right; | + | |
| - | -webkit-user-select: none; | + | |
| - | -moz-user-select: none; | + | |
| - | -ms-user-select: none; | + | |
| - | user-select: none; | + | |
| - | white-space: nowrap; | + | |
| - | width: 1%; | + | |
| - | } | + | |
| - | + | ||
| - | <code> | + | |
| </code> | </code> | ||
| - | ''$ psql -U postgres -W'' | + | В-третьих, переключитесь на базу данных, например, 'dvdrental': |
| - | <code> | + | <code bash> |
| + | postgres=# \c dvdrental You are now connected to database "dvdrental" as user "postgres". | ||
| </code> | </code> | ||
| - | The ''-U'' flag stands for the **u**ser and ''-W'' option requires you to provide the password. In this command, you use the ''postgres'' user to log in to the PostgreSQL database server. | + | <note> |
| - | + | Обратите внимание, что вы можете подключиться к определенной базе данных при входе на сервер базы данных PostgreSQL: | |
| - | Secodn, enter the password for the user ''postgres'' and press the ''Enter'' keywboard: | + | </note> |
| - | + | <code bash> | |
| - | <code> | + | $ psql -U postgres -d dvdrental |
| </code> | </code> | ||
| - | ''Password for user postgres: postgres=#'' | + | В этой команде флаг '' -d '' означает ** d ** atabase. В этой команде вы подключаетесь к базе данных dvdrental с помощью пользователя postgres. |
| + | В-третьих, используйте команду \\ dt из командной строки PostgreSQL для отображения таблиц в базе данных dvdrental: | ||
| - | <code> | + | <code bash> |
| - | Code language: PHP (php) | + | 'postgres=# \dt |
| - | </code> | + | |
| - | + | ||
| - | Third, switch to a database e.g.., ''dvdrental'': | + | |
| - | + | ||
| - | <code> | + | |
| - | </code> | + | |
| - | + | ||
| - | ''postgres=# \c dvdrental You are now connected to database "dvdrental" as user "postgres".'' | + | |
| - | + | ||
| - | <code> | + | |
| - | Code language: PHP (php) | + | |
| - | </code> | + | |
| - | + | ||
| - | Note that you can connect to a specific database when you log in to the PostgreSQL database server: | + | |
| - | + | ||
| - | <code> | + | |
| - | </code> | + | |
| - | + | ||
| - | ''$ psql -U postgres -d dvdrental'' | + | |
| - | + | ||
| - | <code> | + | |
| - | </code> | + | |
| - | + | ||
| - | In this command, ''-d'' flag means **d**atabase. In this command, you connect to the ''dvdrental'' datbase using the ''postgres'' user. | + | |
| - | + | ||
| - | Third, use the ''\dt'' command from the PostgreSQL command prompt to show tables in the ''dvdrental'' database: | + | |
| - | + | ||
| - | <code> | + | |
| - | </code> | + | |
| - | + | ||
| - | ''postgres=# \dt'' | + | |
| - | + | ||
| - | <code> | + | |
| - | Code language: PHP (php) | + | |
| </code> | </code> | ||
| Output: | Output: | ||
| - | {{https://www.postgresqltutorial.com/wp-content/uploads/2020/07/PostgreSQL-show-tables-in-psql-using-dt.png?397x383}}{{https://www.postgresqltutorial.com/wp-content/uploads/2020/07/PostgreSQL-show-tables-in-psql-using-dt.png?397x383}}To get more information on tables, you can use the ''\dt+'' command. It will add the ''size'' and ''description'' columns: | + | {{https://www.postgresqltutorial.com/wp-content/uploads/2020/07/PostgreSQL-show-tables-in-psql-using-dt.png?397x383}} \\ |
| + | \\ | ||
| + | {{https://www.postgresqltutorial.com/wp-content/uploads/2020/07/PostgreSQL-show-tables-in-psql-using-dt.png?397x383}}\\ | ||
| - | <code> | + | Чтобы получить дополнительную информацию о таблицах, вы можете использовать команду '' \\ dt + ''. Он добавит столбцы 'размер' и 'описание':\\ |
| - | </code> | + | |
| - | ''postgres=# \dt+'' | + | <code bash> |
| - | + | postgres=# \dt+ | |
| - | <code> | + | |
| - | Code language: PHP (php) | + | |
| </code> | </code> | ||
| + | \\ | ||
| + | {{https://www.postgresqltutorial.com/wp-content/uploads/2020/07/PostgreSQL-show-tables-in-psql.png?643x386}}\\ | ||
| + | {{https://www.postgresqltutorial.com/wp-content/uploads/2020/07/PostgreSQL-show-tables-in-psql.png?643x386}}\\ | ||
| - | {{https://www.postgresqltutorial.com/wp-content/uploads/2020/07/PostgreSQL-show-tables-in-psql.png?643x386}}{{https://www.postgresqltutorial.com/wp-content/uploads/2020/07/PostgreSQL-show-tables-in-psql.png?643x386}}===== PostgreSQL show tables using pg_catalog schema ===== | + | ===== PostgreSQL show tables using pg_catalog schema ===== |
| - | Another way to show tables in PostgreSQL is to use the ''[[https://www.postgresqltutorial.com/postgresql-select/|SELECT]]'' statement to query data from the PostgreSQL catalog as follows: | + | Другой способ показать таблицы в PostgreSQL - использовать оператор SELECT для запроса данных из каталога PostgreSQL следующим образом: |
| - | <code> | + | <code bash> |
| + | SELECT * FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname != 'information_schema'; | ||
| </code> | </code> | ||
| - | ''SELECT * FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname != 'information_schema';'' | + | {{https://www.postgresqltutorial.com/wp-content/uploads/2020/07/PostgreSQL-show-tables.png?821x454}}\\ |
| - | + | {{https://www.postgresqltutorial.com/wp-content/uploads/2020/07/PostgreSQL-show-tables.png?821x454}}\\ | |
| - | <code> | + | В этом запросе мы использовали условие в предложении WHERE для фильтрации системных таблиц. Если вы опустите предложение WHERE, вы получите много таблиц, включая системные таблицы. |
| - | Code language: SQL (Structured Query Language) (sql) | + | |
| - | </code> | + | |
| - | + | ||
| - | {{https://www.postgresqltutorial.com/wp-content/uploads/2020/07/PostgreSQL-show-tables.png?821x454}}{{https://www.postgresqltutorial.com/wp-content/uploads/2020/07/PostgreSQL-show-tables.png?821x454}}In this query, we used a condition in the ''[[https://www.postgresqltutorial.com/postgresql-where/|WHERE]]'' clause to filter system tables. If you omit the ''WHERE'' clause, you will get many tables including the system tables. | + | |
| - | + | ||
| - | ===== Summary ===== | + | |
| - | + | ||
| - | * Use the ''\dt'' or ''\dt+'' command in ''psql'' to show tables in a specific database. | + | |
| - | * Use the ''SELECT'' statement to query table information from the ''pg_catalog.pg_tables'' catalog. | + | |
| - | + | ||
| - | Was this tutorial helpful ? | + | |
| - | + | ||
| - | Yes No | + | |
| - | |||
| - | window.googletag = window.googletag || {cmd: []}; googletag.cmd.push(function() { | ||
| - | googletag.defineSlot%%('/%%21849154601,22306944310/Ad.Plus-336x280', %%[[%%336, 280], [200, 200], [320, 100], [240, 133], [300, 100], [120, 240], [180, 150], [320, 250], [250, 250], [300, 250], [300, 75]], 'Ad.Plus-336x280-2').addService(googletag.pubads()) | ||
| - | .setTargeting%%('%%site', %%['%%postgresqltutorial.com']); | ||
| - | googletag.enableServices(); googletag.display%%('%%Ad.Plus-336x280-2'); }); | ||