| PostgreSQL 7.4.30 Documentation | ||||
|---|---|---|---|---|
| Prev | Fast Backward | Fast Forward | Next | |
The system catalogs are the place where a relational database management system stores schema metadata, such as information about tables and columns, and internal bookkeeping information. PostgreSQL's system catalogs are regular tables. You can drop and recreate the tables, add columns, insert and update values, and severely mess up your system that way. Normally, one should not change the system catalogs by hand, there are always SQL commands to do that. (For example, CREATE DATABASE inserts a row into the pg_database catalog --- and actually creates the database on disk.) There are some exceptions for particularly esoteric operations, such as adding index access methods.
Table 43-1 lists the system catalogs. More detailed documentation of each catalog follows below.
Most system catalogs are copied from the template database during database creation and are thereafter database-specific. A few catalogs are physically shared across all databases in a cluster; these are marked in the descriptions of the individual catalogs.
Table 43-1. System Catalogs
| Catalog Name | Purpose |
|---|---|
| pg_aggregate | aggregate functions |
| pg_am | index access methods |
| pg_amop | access method operators |
| pg_amproc | access method support procedures |
| pg_attrdef | column default values |
| pg_attribute | table columns ("attributes") |
| pg_cast | casts (data type conversions) |
| pg_class | tables, indexes, sequences ("relations") |
| pg_constraint | check constraints, unique constraints, primary key constraints, foreign key constraints |
| pg_conversion | encoding conversion information |
| pg_database | databases within this database cluster |
| pg_depend | dependencies between database objects |
| pg_description | descriptions or comments on database objects |
| pg_group | groups of database users |
| pg_index | additional index information |
| pg_inherits | table inheritance hierarchy |
| pg_language | languages for writing functions |
| pg_largeobject | large objects |
| pg_listener | asynchronous notification support |
| pg_namespace | schemas |
| pg_opclass | index access method operator classes |
| pg_operator | operators |
| pg_proc | functions and procedures |
| pg_rewrite | query rewrite rules |
| pg_shadow | database users |
| pg_statistic | planner statistics |
| pg_trigger | triggers |
| pg_type | data types |
If you need something similar to the DESCRIBE command available in MySQL or Oracle in order to get information about the columns of a table, then you can do something like:
SELECT
-- Field
pg_attribute.attname AS "Field",
-- Type
CASE pg_type.typname
WHEN 'int2' THEN 'smallint'
WHEN 'int4' THEN 'int'
WHEN 'int8' THEN 'bigint'
WHEN 'varchar' THEN 'varchar(' || pg_attribute.atttypmod-4 || ')'
ELSE pg_type.typname
END AS "Type",
-- Null
CASE WHEN pg_attribute.attnotnull THEN ''
ELSE 'YES'
END AS "Null",
-- Default
CASE pg_type.typname
WHEN 'varchar' THEN substring(pg_attrdef.adsrc from '^\'(.*)\'.*$')
ELSE pg_attrdef.adsrc
END AS "Default"
FROM pg_class
INNER JOIN pg_attribute
ON (pg_class.oid=pg_attribute.attrelid)
INNER JOIN pg_type
ON (pg_attribute.atttypid=pg_type.oid)
LEFT JOIN pg_attrdef
ON (pg_class.oid=pg_attrdef.adrelid AND pg_attribute.attnum=pg_attrdef.adnum)
WHERE pg_class.relname='table_name' AND pg_attribute.attnum>=1 AND NOT pg_attribute.attisdropped
ORDER BY pg_attribute.attnum;
where table_name is the name of the table (relation) whose columns you want to get information about.
However be aware that, unlike the MySQL DESCRIBE command, if there is no table named table_name, then the above SELECT command will simply display 0 row (no error).
Ooops... sorry! The above query gives:
ERROR: syntax error at or near "(" at character 386
Replace line 18
WHEN 'varchar' THEN substring(pg_attrdef.adsrc from '^'(.*)'.*$')
by
WHEN 'varchar' THEN substring(pg_attrdef.adsrc from '^\'(.*)\'.*$')
and it should work fine.