PostgreSQL XML Type

In this post I am going to demonstrate the XML type of PostgreSQL.

Explain with sample example.

What is XML Type ?

The XML Data type is used to store XML data. XML is well formed storage structure of data. You can store multiple node into a single XML column. You can store data into hierarchical format.


User preferences table, in which User can select multiple preferences and even User can create custom preferences for them.

Now how you can store this preferences data for those Users.

Simple, create one XML column and store all User preferences id into this. like,


Below is a demonstration of XML with PostgreSQL. 

Basically you can store XML data with one child node or with different child node.

Les’s First start with one child node:
Create table with XML type:

Now Insert XML Data.

Select data from XMLTEST:

You can find above full XML into one column.


Now time to parse this XML column using unnest() function of PostgreSQL.

Result is:


You can see this is four rows now, as we have stored different four values into one XML Column and after parse we can see those four values in the form of different row.

Demonstration of different child node:

Result is :


In above code as we have inserted one XML data with different child node.

You can see the result of one row with different column for different child node.

Other Related Articles,

How to create job in PostgreSQL.

Anvesh Patel

Leave a Reply

3 Comments on "PostgreSQL XML Type"

Notify of
Sort by:   newest | oldest | most voted

Thanks Anvesh,
good article !
Really You solved my problem for postgres XML.


can you give an example of a query that involve where clause in the xpath ?