Project

General

Profile

CreateLang » History » Version 10

Álvaro Herrera, 06/22/2010 10:11 AM

1 9 Álvaro Herrera
h1. Creating the PL/php language in a PostgreSQL database
2 8 Álvaro Herrera
3
4 1 Álvaro Herrera
The first thing you need is to have a PL/php entry in the pg_pltemplate system catalog.
5 6 Álvaro Herrera
6 8 Álvaro Herrera
In *8.1 and 8.2* you can insert one by issuing, as a superuser:
7
<pre>
8 5 Álvaro Herrera
INSERT INTO pg_pltemplate VALUES
9 1 Álvaro Herrera
('plphp', 't', 'plphp_call_handler', 'plphp_validator', '$libdir/plphp', NULL);
10
11 5 Álvaro Herrera
INSERT INTO pg_pltemplate VALUES
12 1 Álvaro Herrera
('plphpu', 'f', 'plphp_call_handler', 'plphp_validator', '$libdir/plphp', NULL);
13
</pre>
14 6 Álvaro Herrera
15 9 Álvaro Herrera
Note that for *8.3 and later* you need this instead:
16 8 Álvaro Herrera
<pre>
17 10 Álvaro Herrera
INSERT INTO pg_pltemplate (tmplname, tmpltrusted, tmpldbacreate, tmplhandler, tmplvalidator, tmpllibrary) VALUES
18
('plphp', 't', 't', 'plphp_call_handler', 'plphp_validator', '$libdir/plphp');
19 6 Álvaro Herrera
20 10 Álvaro Herrera
INSERT INTO pg_pltemplate(tmplname, tmpltrusted, tmpldbacreate, tmplhandler, tmplvalidator, tmpllibrary) VALUES
21
('plphpu', 'f', 'f', 'plphp_call_handler', 'plphp_validator', '$libdir/plphp');
22 8 Álvaro Herrera
</pre>
23 5 Álvaro Herrera
24 9 Álvaro Herrera
Note you *DON'T* have to edit the @$libdir@.  Leave it alone.  It will be expanded by PostgreSQL automatically.  You don't need to add the @.so@ suffix either (or whatever it's called on your platform.)
25 1 Álvaro Herrera
26
This will create entries for the trusted and untrusted versions of PL/php.  Note that pg_pltemplate is a shared catalog, which means you have to do it only once in any database and it will be available in all your databases automatically.
27
28
In any database where you want to use PL/php you need to issue simply
29
30 8 Álvaro Herrera
<pre>
31 1 Álvaro Herrera
CREATE LANGUAGE plphp;
32 8 Álvaro Herrera
</pre>
33 1 Álvaro Herrera
34
or
35 8 Álvaro Herrera
<pre>
36 1 Álvaro Herrera
CREATE LANGUAGE plphpu;
37 8 Álvaro Herrera
</pre>
38 2 Álvaro Herrera
(the latter will create the untrusted version, which by default only superusers can write functions in).
39 5 Álvaro Herrera
40 1 Álvaro Herrera
Now the language is ready to be used.
41
42
If you receive an error similar to:
43
44 8 Álvaro Herrera
<pre>
45 2 Álvaro Herrera
ERROR:  could not load library "/usr/local/lib/postgresql/plphp":
46 1 Álvaro Herrera
libphp4.so: cannot open shared object file: No such file or directory
47 8 Álvaro Herrera
</pre>
48 6 Álvaro Herrera
49 1 Álvaro Herrera
it means the Postmaster can't find the PHP shared library.  You should probably define a LD_LIBRARY_PATH variable in the postmaster environment that points to the location where libphp.so is located (or add that path to /etc/ld.so.conf)