Selezionare il tipo di dati del campo in Postgres
-
23-09-2019 - |
Domanda
Come faccio ad avere il tipo di dati di campo specifico dalla tabella in Postgres? Per esempio Ho la seguente tabella, student_details ( integer stu_id, stu_name varchar (30), timestamp JOINED_DATE );
In questo utilizzando il nome del campo / o qualsiasi altro modo, ho bisogno di ottenere il tipo di dati del campo specifico. C'è qualche possibilità?
Nessuna soluzione corretta
Altri suggerimenti
È possibile ottenere i tipi di dati dal information_schema (8.4 docs riferimento qui, ma questa non è una novità):
=# select column_name, data_type from information_schema.columns
-# where table_name = 'config';
column_name | data_type
--------------------+-----------
id | integer
default_printer_id | integer
master_host_enable | boolean
(3 rows)
È possibile utilizzare il pg_typeof ( ) funzione, che funziona bene anche per valori arbitrari.
SELECT pg_typeof("stu_id"), pg_typeof(100) from student_details limit 1;
psql -E
correre e poi \d student_details
Prova questa richiesta:
SELECT column_name, data_type FROM information_schema.columns WHERE
table_name = 'YOUR_TABLE' AND column_name = 'YOUR_FIELD';
Se ti piace la soluzione 'Mike Sherrill', ma non si desidera utilizzare psql, ho usato questa query per ottenere le informazioni mancanti:
select column_name,
case
when domain_name is not null then domain_name
when data_type='character varying' THEN 'varchar('||character_maximum_length||')'
when data_type='numeric' THEN 'numeric('||numeric_precision||','||numeric_scale||')'
else data_type
end as myType
from information_schema.columns
where table_name='test'
con esito:
column_name | myType
-------------+-------------------
test_id | test_domain
test_vc | varchar(15)
test_n | numeric(15,3)
big_n | bigint
ip_addr | inet
I punti di vista dello schema di informazione e pg_typeof () restituiscono informazioni tipo incompleto. Di queste risposte, psql
fornisce le informazioni di tipo più precise. (Il PO potrebbe non avere bisogno di tali informazioni precise, ma dovrebbe conoscere i limiti.)
create domain test_domain as varchar(15);
create table test (
test_id test_domain,
test_vc varchar(15),
test_n numeric(15, 3),
big_n bigint,
ip_addr inet
);
Uso psql
e \d public.test
correttamente mostra l'uso del tipo di dati test_domain
, la lunghezza del varchar (n) colonne, e la precisione e la scala numerica (p, s) colonne.
sandbox=# \d public.test Table "public.test" Column | Type | Modifiers ---------+-----------------------+----------- test_id | test_domain | test_vc | character varying(15) | test_n | numeric(15,3) | big_n | bigint | ip_addr | inet |
Questa query su una vista information_schema non visualizza l'uso di test_domain
a tutti. Inoltre non riporta i dettagli di varchar (n) e (p, s) colonne numeriche.
select column_name, data_type
from information_schema.columns
where table_catalog = 'sandbox'
and table_schema = 'public'
and table_name = 'test';
column_name | data_type -------------+------------------- test_id | character varying test_vc | character varying test_n | numeric big_n | bigint ip_addr | inet
potrebbe essere in grado di ottenere tutte le informazioni che unendo altre viste INFORMATION_SCHEMA, o interrogando direttamente le tabelle di sistema. psql -E
potrebbe aiutare in questo.
La funzione pg_typeof()
mostra correttamente l'uso di test_domain
, ma non segnala i dettagli di varchar (n) e (p, s) colonne numeriche.
select pg_typeof(test_id) as test_id,
pg_typeof(test_vc) as test_vc,
pg_typeof(test_n) as test_n,
pg_typeof(big_n) as big_n,
pg_typeof(ip_addr) as ip_addr
from test;
test_id | test_vc | test_n | big_n | ip_addr -------------+-------------------+---------+--------+--------- test_domain | character varying | numeric | bigint | inet
infilaggio tipo di dati da information_schema
è possibile, ma non conveniente (richiede unire parecchie colonne con un'istruzione case
). In alternativa si può usare la funzione incorporata format_type
farlo, ma funziona su identificatori di tipo interne che sono visibili in pg_attribute
ma non in information_schema
. Esempio
SELECT a.attname as column_name, format_type(a.atttypid, a.atttypmod) AS data_type
FROM pg_attribute a JOIN pg_class b ON a.attrelid = b.relfilenode
WHERE a.attnum > 0 -- hide internal columns
AND NOT a.attisdropped -- hide deleted columns
AND b.oid = 'my_table'::regclass::oid; -- example way to find pg_class entry for a table
In base a https://gis.stackexchange.com/a/97834 .