hi all,
How to get fieldnames of a table using postgresql?
Can someone help me to overcome this issue?
Like if i have table called "employee" i need to fetch the fieldnames(id,name,workstatus..etc) of employees alone not its records...Is any method to get this?
Thanks in advance...
9 46608
If it does not have to be a query the simplest approach is to use the metacommand \d in psql session: -
postgres=# \d bar
-
Table "public.bar"
-
Column | Type | Modifiers
-
--------+-----------------------+--------------------------------------------------
-
id | integer | not null default nextval('bar_id_seq'::regclass)
-
f1 | character varying(32) |
-
f2 | character varying(32) |
-
Indexes:
-
"bar_pkey" PRIMARY KEY, btree (id)
-
Triggers:
-
trig_audit BEFORE INSERT OR UPDATE ON bar FOR EACH ROW EXECUTE PROCEDURE audit_proc()
-
\d without a table name will give you a list of all user tables in the database.
See psql manual for details
Thanks michael for your help..
I need a query to fetch the fieldnames is any method available for that?
If so guide me the solution or its related pages...
Regards..
Barani
I am pretty sure this question came up before, I remember I posted the sql query that gives this info few months ago.
Try to search the Postgresql forum, if you found the answer post a link to it - it may help others.
Meanwhile take a look at the Postgresql Information Schema
Hi michael,
I have gone through your guidance and searched perl forum...
You have mentioned the query as "select count(*) from tablename"
Its not giving me the fieldnames of a table...
Kindly suggest me some other way...
Thanks&Regards,
Barani..
Hi michael,
I have gone through your guidance and searched perl forum...
You have mentioned the query as "select count(*) from tablename"
Its not giving me the fieldnames of a table...
No, no I said:
>> Try to search the Postgresql forum ...
"select count(*) from tablename" is not what you need, this query only returns the number of rows in the table.
I looked at the old postings, but could not find anything, which is strange, I remember replying to the same question few months ago.
I'll try to dig it out for you.
This works for me: -
SELECT
-
a.attname as "Column",
-
pg_catalog.format_type(a.atttypid, a.atttypmod) as "Datatype"
-
FROM
-
pg_catalog.pg_attribute a
-
WHERE
-
a.attnum > 0
-
AND NOT a.attisdropped
-
AND a.attrelid = (
-
SELECT c.oid
-
FROM pg_catalog.pg_class c
-
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
-
WHERE c.relname ~ '^(foo)$'
-
AND pg_catalog.pg_table_is_visible(c.oid)
-
)
-
;
-
Replace foo on line 13 with the name of your table.
The code should produce the output similar to this: -
Column | Datatype
-
--------+-----------------------
-
id | integer
-
fs1 | character varying(32)
-
fi2 | integer
-
fn3 | numeric(5,0)
-
(4 rows)
-
If you really need this functionality consider making a function that would take the table name as an argument and spit out the result.
This works for me: -
SELECT
-
a.attname as "Column",
-
pg_catalog.format_type(a.atttypid, a.atttypmod) as "Datatype"
-
FROM
-
pg_catalog.pg_attribute a
-
WHERE
-
a.attnum > 0
-
AND NOT a.attisdropped
-
AND a.attrelid = (
-
SELECT c.oid
-
FROM pg_catalog.pg_class c
-
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
-
WHERE c.relname ~ '^(foo)$'
-
AND pg_catalog.pg_table_is_visible(c.oid)
-
)
-
;
-
Replace foo on line 13 with the name of your table.
The code should produce the output similar to this: -
Column | Datatype
-
--------+-----------------------
-
id | integer
-
fs1 | character varying(32)
-
fi2 | integer
-
fn3 | numeric(5,0)
-
(4 rows)
-
If you really need this functionality consider making a function that would take the table name as an argument and spit out the result.
Hi michael,
Thanks for your help and cooperation towards my query...
Its working for me now...i have searched in the postgresql forum but i have not found this type of query over there ...
Any way i like to thank you once again for your timely help...
Hope we will have furhermore discussions..
Regards,
Barani...
Hi michael,
The query you have mentioned me is working and giving me the result what i needed...
But i need to understand its structure, in the previous reply u have mentioned about information schema...
Is that is related with the query you have posted...
Kindly post me the details...
With regards,
Barani...
I did not find any such query. Your query works for me.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Bart Van der Donck |
last post by:
Hello,
I am using MySQL 4.0.
Say that I have a table named "mytable" having a column "ID" and a
column "test columnname".
When dumping:
mysqldump --opt DATABASE -uUSER -hHOST -pPASS >...
|
by: Matthew Cascio |
last post by:
My understanding is that using reserved words as column names is
allowable as long as they are quoted. I am trying to create a table
dynamically with columns defined by the first row of a text...
|
by: Tyler Hudson |
last post by:
/*Code below raises following errors:
Server: Msg 245, Level 16, State 1, Line 6
Syntax error converting the varchar value 'a' to a column of data type
int.
*/
create table #x (i integer, c...
|
by: noor |
last post by:
Hi
I have been working since 2 days to device a method to export sql table
into csv format. I have tried using bcp with format option to keep the
column names but I'm unable to transfer the file...
|
by: John.Arthur |
last post by:
Hi,
I am learning Ms Sql and I found that a lot of the tables I am finding
( in different tutorials ) are using special names for their column
names such like au_username, au_salery ...
Is the...
|
by: Joe |
last post by:
Hi All,
I am new to using the Access DB and I need some help if someone
is able to give it to me. What I want to do is get the names of
the columns of certain tables. Not the data in the table...
|
by: Larry Bird |
last post by:
I've created a AlertDataClass below within the class I have tables and column that I've create. In the AlertDataAccess class I'm trying to insert data into my tables. AlertDataAccess is a Module...
|
by: natG |
last post by:
Hi;
I am transferring data from MySql to db2 using my own java/jdbc program.
Working out ok, except for the fact that our apps use mixed-case names for
tables and columns. Although my CREATE TABLE...
|
by: christianlott1 |
last post by:
I want to provide users with an interface to create a custom merge
(all in Access, not Word). User will put in a set of brackets ("<>")
in a memo field and when they click the merge button it will...
|
by: beautifulcarcass |
last post by:
Hi, in this school project im making,
im having a problem if i could display the column names from a table on a MYSQL database to a webpage through PHP with a loop
is there a function to display...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |