I'm looking for a way to run a query to find the default values of the columns of a table in Postgres. For example, if I made a table with the following query:
**Editor's note: I fixed the table definition as it has no impact on the question.
CREATE TABLE mytable (
integer int DEFAULT 2,
text varchar(64) DEFAULT 'I am default',
moretext varchar(64) DEFAULT 'I am also default',
unimportant int
);
I need a query that would tell me, in some format, that the default for integer
is 2, text
is 'I am default', and moretext
is 'I am also default'. The query result can include any value for any other column that doesn't have a default, i.e., unimportant
is unimportant for my purposes and doesn't matter at all.
table, such as the default value of a column.
--create table
--show table information
Use the information schema:
Up to the schema naming, this should work in any SQL database system.
@Zohaib's query is almost but not quite right. There are a couple of issues. I copied it into my answer for future reference. Do not use this:
He copied from some blog. That he mentions it is good. But in such a case the source should be added. People reading that blog need to be warned.
Typo in
pg_atttribute
- fixed easily.Doesn't return any rows, if there is no default specified for the requested column. Better make that a
LEFT JOIN pg_attrdef ON ..
, so you always get a resulting row if the column exists. It will be NULL, if there is no default, which is actually the correct result becauseNULL
is the default then.If you remove
attname
from the WHERE clause, you only get values for columns that actually have a default value. Not for others. And you need to addattname
to the SELECT list or you will not know for which column.The query would also return the default of a column that is already dropped, which is wrong. Read about the details in the manual.
But most importantly: the query can give completely wrong results, as it does not take the schema name into account. There can be any number of
table1.col1
in a postgres database: in various schemas. If more than one have a default, you get multiple values. If the column you have in mind does not have a default, but another one in another schema does, you will be fooled and never know it.To sum it up:
Copy / pasting from some blog without insight went dangerously wrong!
Try this instead:
The
LEFT JOIN
makes sure you get a result as long as the column exists. If you want to exclude those, make it aJOIN
instead. But be prepared to get no rows occasionally.Be aware that the special cast
::regclass
takes the current setting forsearch_path
into account, so even if you do not include the schema in the name (which you should, to be sure!), chances are you get the expected result. Read more about it in the manual.Including
pg_class
is redundant, once we have the OID of the table. Skip it and speed up query.I liked Erwin's Answer but was having some difficulties:
NOT a.attisdropped
qualification. It was not consistent, I wound up having to loop through the results and check the column names.'I am default'::character varying
which did not work when they are being stuffed into web form input values. I could not think of a good way to remove the cast suffix without doing something like.replace(/::.*/, '')
which is not robust enough. I bet Erwin can figure out some magical way toEVAL()
the returned value and use thea.atttypid
column to get the right data type.So I went back to what I was doing before:
One thing to note here is that any
SERIAL
columns will get incremented. It likely does not matter so long as it is unique if it is just a table index. Otherwise it is a deal breaker.The other thing to watch out for is any
TRIGGER AFTER/BEFORE INSERT
which will fire even though theINSERT
gets rolled back (I think any changes the trigger function makes will get rolled back though.)I found this query for postgresql on one of the blogs. you can try this to check if it works. To get values of all coumns, you can try removing
AND pat.attname='your_column_name'
from where clause.