Skip to content
Advertisement

Set default value for all columns of a table

I have multiple tables with a lot of columns. Some columns contain Null values. Now, i want to set them to a default value (empty string or 0).

I found two two pieces of SQL that probably cover what i need but i can’t put them together.

I can use the above code to get all columns and their data type and the snippet below to update Null values in columns.

How can i put these two together? Sorry, if this is a basic question but i couldn’t find a solution for this…

Advertisement

Answer

Surely, this is not basic question, and solution is little bit hard (not for experts). You have to generate SQL statement from system data stored in table pg_attribute and pg_attrdef.

You can run a following query. The result is text of UPDATE command, that does what you want:

Example:

run query and you get:

Now you can run:

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement