Getting fields in a table through a query?

Started by Mitch Vincentabout 24 years ago3 messagesgeneral
Jump to latest
#1Mitch Vincent
mitch@doot.org

How can I get the fields in a given table via an SQL query even if there are
no records in that table? I just need the names of all the fields in a
table -- is that even possible?

Thanks!

-Mitch

#2Stephan Szabo
sszabo@megazone23.bigpanda.com
In reply to: Mitch Vincent (#1)
Re: Getting fields in a table through a query?

On Mon, 4 Feb 2002, Mitch Vincent wrote:

How can I get the fields in a given table via an SQL query even if there are
no records in that table? I just need the names of all the fields in a
table -- is that even possible?

select * from pg_attribute, pg_class where attrelid=pg_class.oid and
relname='<table name>' and attnum>0;
should give you all the user columns on the table specified as <table
name>

(If you want to get system columns like oid, you can leave off the
attnum>0)

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Mitch Vincent (#1)
Re: Getting fields in a table through a query?

"Mitch Vincent" <mitch@doot.org> writes:

How can I get the fields in a given table via an SQL query even if there are
no records in that table? I just need the names of all the fields in a
table -- is that even possible?

Why not just
select * from foo where false;
and examine the column names that come back? Lack of any data will not
stop the system from sending column headers.

regards, tom lane