How to convert a python array into rows in PostgreSQL

If you have a list of values that you would like to convert into a set of rows in postgresql, you can use the unnest function:

values = ['foo', 'bar']

result = cur.execute(
    '''
    SELECT unnest(%(values)s)
    ''',
    {
        'values': ['foo', 'bar']
    }
)

print(result.fetchall())

This will output:

>>> print(result.fetchall())
[('foo',), ('bar',)]

Comments

Add Comment

Name

Email

Comment

Are you human? + seven = 14