I explained in my previous articles that PostgreSQL can be written in more than one language, In this article, I will explain the Python example.
Python is one of the programming languages that has been in our lives for a long time and has many useful libraries.
In order to write Python code on PostgreSQL, you need to install the “plpythonu” extension.
You can install the “plpythonu” extension by running the following command in the database in which we want to write the Python code and start writing Python codes thanks to this extension.
1 |
CREATE EXTENSION plpythonu |
After the extension is installed, let’s see the function in which the Python command is written.
1 2 3 4 5 |
CREATE FUNCTION composte_type_as_list() RETURNS type_record[] AS $$ return [[('first', 1), ('second', 1)], [('first', 2), ('second', 2)], [('first', 3), ('second', 3)]]; $$ LANGUAGE plpythonu; |
We can use the following command to run the above function.
1 |
SELECT * FROM composte_type_as_list(); |