SELECT
column_name,
data_type
FROM
information_schema.columns
WHERE
table_name = 'table_name';
with the above query you can retrieve columns and its datatype.
Answer from selva on Stack OverflowSELECT
column_name,
data_type
FROM
information_schema.columns
WHERE
table_name = 'table_name';
with the above query you can retrieve columns and its datatype.
Open psql command line and type :
\d+ table_name
In addition to the command line \d+ <table_name> you already found, you could also use the Information Schema to look up the column data, using information_schema.columns:
SELECT *
FROM information_schema.columns
WHERE table_schema = 'your_schema'
AND table_name = 'your_table'
;
Note: As per the example above, make sure the values are enclosed within quotes.
As a supplement to the other answers, even a SELECT statement that returns no rows will expose the column names to you and to application code.
select *
from table_name
where false;
I intend this to be executable by almost any client software for almost any DBMS. (Almost? Yes, some don't support a clause like where false. Instead, they require an expression like where 1 = 0.)
Permissions might come into play with any of these approaches.
Does Coefficient work with both Google Sheets and Excel?
Can I export data back to my business systems with Coefficient?
How does Coefficient's automated refresh work?
I don't know why you would ever need to do this, but it is possible using some of the JSON functions introduced in 9.3.
SELECT json_object_keys(row_to_json(t)) FROM
(SELECT * FROM table1
LEFT JOIN table2 ON table1.id = table2.tbl1_id LIMIT 1) t;
This will give you the name of every column returned for a single row. Without the LIMIT you would get the columns repeated for every row returned. If you wanted to see the values returned as well you can get more complex:
WITH t as
(SELECT * FROM table1
LEFT JOIN table2 ON table1.id = table2.tbl1_id LIMIT 1)
SELECT json_data.key, json_data.value
FROM t, json_each_text(row_to_json(t)) AS json_data;
Both these queries will return all the columns even if they are named the same. If all you want is a list of unique column names, you can utilize hstore:
CREATE EXTENSION hstore; --Create the extension if you need it.
SELECT akeys(hstore(t)) as array_of_columns
FROM
(SELECT * FROM table1
LEFT JOIN table2 ON table1.id = table2.tbl1id LIMIT 1) t;
The only reasonable approach I can come up with is to do something like this. Schema location plays into this, but I'm just posting the basic idea. SQL doesn't have reflection at run time unless you go and hit meta data tables, which is what you seem to be trying to achieve here.
SELECT table1.*
INTO x
FROM table1
LEFT JOIN table2
ON table1.id = table2.tbl1_id LIMIT 0;
select *
from information_schema.columns
where table_Name = 'x';
Also interesting enough while traveling the Internets over lunch I ran into this. Works with PostgreSQL 8.2.0 or higher.
https://github.com/theory/colnames/tree/master/test/sql
With Postgres (and its JDBC driver) you can do the following:
PreparedStatement pstmt = con.prepareStatement("select ... ");
ResultSetMetaData meta = pstmt.getMetaData();
for (int i=1; i <= meta.getColumnCount(); i++)
{
System.out.println("Column name: " + meta.getColumnName(i) + ", data type: " + meta.getColumnTypeName(i));
}
Note that you do not need to add a where false or limit 0 to the statement. The call to prepareStatement() does not actually execute the query.
Given an SQL statement as a string, could you not add " LIMIT=0" to the string and run the modified query? Of course this would only get the column names, but these could then be used to query the information schema for the types.
If the SQL already contains a limit, its argument could be modified?