You can use this query to find the table information:
SELECT a.attnum, a.attname AS field, t.typname AS type, a.attlen AS length, a.atttypmod AS lengthvar, a.attnotnull AS notnull FROM pg_class c, pg_attribute a, pg_type t WHERE c.relname = 'your_table_name' AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid ORDER BY a.attnum
debugginginternalsmetadatapostgresqlqueriessql
Use the following query to show all tables without primary keys:
SELECT
nspname AS schema,
relname AS table
FROM pg_class
LEFT JOIN pg_constraint ON
pg_constraint.contype = 'p' AND
conrelid = pg_class.oid
JOIN pg_namespace ON
pg_namespace.oid = pg_class.relnamespace
WHERE
relkind = 'r' AND contype IS NULL;
internalskeysmetadatapostgresqlprimarysql
Given a table MYTABLE in namespace MYNAMESPACE, you can use this SQL to retrieve the primary key column name(s) for the table:
SELECT attname::text FROM pg_attribute JOIN pg_class ON pg_attribute.attrelid = pg_class.oid JOIN pg_namespace ON pg_namespace.oid = pg_class.relnamespace LEFT JOIN pg_constraint ON conrelid = pg_class.oid AND pg_constraint.contype = 'p' WHERE pg_namespace.nspname = 'MYNAMESPACE' AND pg_class.relname = 'MYTABLE' AND pg_attribute.attnum = ANY (pg_constraint.conkey) ORDER BY pg_attribute.attnum;
columnsinternalskeymetadatapostgresqlprimarysql
If you have a database mytemplate that you'd like to use as a template, update its datistemplate attribute in the pg_database relation:
UPDATE pg_database SET datistemplate = 't' WHERE datname = 'mytemplate';
Then you can use it as a template when creating other databases:
CREATE DATABASE otherdb TEMPLATE mytemplate;
debugginggotchainternalspostgresqlrecoveryrestoresqltemplate