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.

Please share your ideas and opinions about this topic with me, your contribution will add true value to this topic.
If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.

If you like this post, then please share it with others.
Please follow, I will share my experience towards the success of Database Research and Development Activity.

I put up a post every day, please keep reading and learning.
Discover Yourself, Happy Blogging !
Anvesh M. Patel.

More from

Leave a Reply

3 Comments on "PostgreSQL XML Type"

Notify of

Sort by:   newest | oldest | most voted
1 year 1 month ago

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

5 months 20 days ago

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