This repository contains an example of a C User-Defined Function (UDF) for PostgreSQL. The UDF takes a string as input and returns a string as output.
string_udf.c: The C source file for the UDF.Makefile: The makefile to compile the UDF into a shared library.string_udf--1.0.sql: The SQL script to create the UDF in PostgreSQL.string_udf.control: The control file for the PostgreSQL extension.Dockerfile: The Dockerfile to build an image with PostgreSQL and compile the UDF.
- PostgreSQL installed and running.
- GCC compiler and Make utility installed.
- Create the Shared Library:
Place
string_udf.c,Makefile,string_udf--1.0.sql, andstring_udf.controlin the same directory. Run the following command to compile the UDF:make sudo make install
-
Connect to PostgreSQL Database: Use
psqlor another PostgreSQL client to connect to your PostgreSQL database. -
Create the UDF: Execute the following SQL command to create the UDF:
CREATE EXTENSION string_udf;
- Test the UDF:
Verify that your UDF is working by calling the function in SQL. Run:
This should return
SELECT string_udf('Hello, World!');
Hello, World!if the UDF is functioning correctly.
This project is licensed under the MIT License - see the LICENSE file for details.