<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>CREATE LANGUAGE</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="sql-createindex.html" title="CREATE INDEX" /><link rel="next" href="sql-creatematerializedview.html" title="CREATE MATERIALIZED VIEW" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">CREATE LANGUAGE</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-createindex.html" title="CREATE INDEX">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><th width="60%" align="center">SQL Commands</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 11.5 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="sql-creatematerializedview.html" title="CREATE MATERIALIZED VIEW">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SQL-CREATELANGUAGE"><div class="titlepage"></div><a id="id-1.9.3.70.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">CREATE LANGUAGE</span></h2><p>CREATE LANGUAGE — define a new procedural language</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis"> CREATE [ OR REPLACE ] [ PROCEDURAL ] LANGUAGE <em class="replaceable"><code>name</code></em> CREATE [ OR REPLACE ] [ TRUSTED ] [ PROCEDURAL ] LANGUAGE <em class="replaceable"><code>name</code></em> HANDLER <em class="replaceable"><code>call_handler</code></em> [ INLINE <em class="replaceable"><code>inline_handler</code></em> ] [ VALIDATOR <em class="replaceable"><code>valfunction</code></em> ] </pre></div><div class="refsect1" id="SQL-CREATELANGUAGE-DESCRIPTION"><h2>Description</h2><p> <code class="command">CREATE LANGUAGE</code> registers a new procedural language with a <span class="productname">PostgreSQL</span> database. Subsequently, functions and procedures can be defined in this new language. </p><div class="note"><h3 class="title">Note</h3><p> As of <span class="productname">PostgreSQL</span> 9.1, most procedural languages have been made into <span class="quote">“<span class="quote">extensions</span>”</span>, and should therefore be installed with <a class="xref" href="sql-createextension.html" title="CREATE EXTENSION"><span class="refentrytitle">CREATE EXTENSION</span></a> not <code class="command">CREATE LANGUAGE</code>. Direct use of <code class="command">CREATE LANGUAGE</code> should now be confined to extension installation scripts. If you have a <span class="quote">“<span class="quote">bare</span>”</span> language in your database, perhaps as a result of an upgrade, you can convert it to an extension using <code class="literal">CREATE EXTENSION <em class="replaceable"><code>langname</code></em> FROM unpackaged</code>. </p></div><p> <code class="command">CREATE LANGUAGE</code> effectively associates the language name with handler function(s) that are responsible for executing functions written in the language. Refer to <a class="xref" href="plhandler.html" title="Chapter 56. Writing A Procedural Language Handler">Chapter 56</a> for more information about language handlers. </p><p> There are two forms of the <code class="command">CREATE LANGUAGE</code> command. In the first form, the user supplies just the name of the desired language, and the <span class="productname">PostgreSQL</span> server consults the <a class="link" href="catalog-pg-pltemplate.html" title="52.37. pg_pltemplate"><code class="structname">pg_pltemplate</code></a> system catalog to determine the correct parameters. In the second form, the user supplies the language parameters along with the language name. The second form can be used to create a language that is not defined in <code class="structname">pg_pltemplate</code>, but this approach is considered obsolescent. </p><p> When the server finds an entry in the <code class="structname">pg_pltemplate</code> catalog for the given language name, it will use the catalog data even if the command includes language parameters. This behavior simplifies loading of old dump files, which are likely to contain out-of-date information about language support functions. </p><p> Ordinarily, the user must have the <span class="productname">PostgreSQL</span> superuser privilege to register a new language. However, the owner of a database can register a new language within that database if the language is listed in the <code class="structname">pg_pltemplate</code> catalog and is marked as allowed to be created by database owners (<code class="structfield">tmpldbacreate</code> is true). The default is that trusted languages can be created by database owners, but this can be adjusted by superusers by modifying the contents of <code class="structname">pg_pltemplate</code>. The creator of a language becomes its owner and can later drop it, rename it, or assign it to a new owner. </p><p> <code class="command">CREATE OR REPLACE LANGUAGE</code> will either create a new language, or replace an existing definition. If the language already exists, its parameters are updated according to the values specified or taken from <code class="structname">pg_pltemplate</code>, but the language's ownership and permissions settings do not change, and any existing functions written in the language are assumed to still be valid. In addition to the normal privilege requirements for creating a language, the user must be superuser or owner of the existing language. The <code class="literal">REPLACE</code> case is mainly meant to be used to ensure that the language exists. If the language has a <code class="structname">pg_pltemplate</code> entry then <code class="literal">REPLACE</code> will not actually change anything about an existing definition, except in the unusual case where the <code class="structname">pg_pltemplate</code> entry has been modified since the language was created. </p></div><div class="refsect1" id="SQL-CREATELANGUAGE-PARAMETERS"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">TRUSTED</code></span></dt><dd><p><code class="literal">TRUSTED</code> specifies that the language does not grant access to data that the user would not otherwise have. If this key word is omitted when registering the language, only users with the <span class="productname">PostgreSQL</span> superuser privilege can use this language to create new functions. </p></dd><dt><span class="term"><code class="literal">PROCEDURAL</code></span></dt><dd><p> This is a noise word. </p></dd><dt><span class="term"><em class="replaceable"><code>name</code></em></span></dt><dd><p> The name of the new procedural language. The name must be unique among the languages in the database. </p><p> For backward compatibility, the name can be enclosed by single quotes. </p></dd><dt><span class="term"><code class="literal">HANDLER</code> <em class="replaceable"><code>call_handler</code></em></span></dt><dd><p><em class="replaceable"><code>call_handler</code></em> is the name of a previously registered function that will be called to execute the procedural language's functions. The call handler for a procedural language must be written in a compiled language such as C with version 1 call convention and registered with <span class="productname">PostgreSQL</span> as a function taking no arguments and returning the <code class="type">language_handler</code> type, a placeholder type that is simply used to identify the function as a call handler. </p></dd><dt><span class="term"><code class="literal">INLINE</code> <em class="replaceable"><code>inline_handler</code></em></span></dt><dd><p><em class="replaceable"><code>inline_handler</code></em> is the name of a previously registered function that will be called to execute an anonymous code block (<a class="xref" href="sql-do.html" title="DO"><span class="refentrytitle">DO</span></a> command) in this language. If no <em class="replaceable"><code>inline_handler</code></em> function is specified, the language does not support anonymous code blocks. The handler function must take one argument of type <code class="type">internal</code>, which will be the <code class="command">DO</code> command's internal representation, and it will typically return <code class="type">void</code>. The return value of the handler is ignored. </p></dd><dt><span class="term"><code class="literal">VALIDATOR</code> <em class="replaceable"><code>valfunction</code></em></span></dt><dd><p><em class="replaceable"><code>valfunction</code></em> is the name of a previously registered function that will be called when a new function in the language is created, to validate the new function. If no validator function is specified, then a new function will not be checked when it is created. The validator function must take one argument of type <code class="type">oid</code>, which will be the OID of the to-be-created function, and will typically return <code class="type">void</code>. </p><p> A validator function would typically inspect the function body for syntactical correctness, but it can also look at other properties of the function, for example if the language cannot handle certain argument types. To signal an error, the validator function should use the <code class="function">ereport()</code> function. The return value of the function is ignored. </p></dd></dl></div><p> The <code class="literal">TRUSTED</code> option and the support function name(s) are ignored if the server has an entry for the specified language name in <code class="structname">pg_pltemplate</code>. </p></div><div class="refsect1" id="SQL-CREATELANGUAGE-NOTES"><h2>Notes</h2><p> Use <a class="xref" href="sql-droplanguage.html" title="DROP LANGUAGE"><span class="refentrytitle">DROP LANGUAGE</span></a> to drop procedural languages. </p><p> The system catalog <code class="classname">pg_language</code> (see <a class="xref" href="catalog-pg-language.html" title="52.29. pg_language">Section 52.29</a>) records information about the currently installed languages. Also, the <span class="application">psql</span> command <code class="command">\dL</code> lists the installed languages. </p><p> To create functions in a procedural language, a user must have the <code class="literal">USAGE</code> privilege for the language. By default, <code class="literal">USAGE</code> is granted to <code class="literal">PUBLIC</code> (i.e., everyone) for trusted languages. This can be revoked if desired. </p><p> Procedural languages are local to individual databases. However, a language can be installed into the <code class="literal">template1</code> database, which will cause it to be available automatically in all subsequently-created databases. </p><p> The call handler function, the inline handler function (if any), and the validator function (if any) must already exist if the server does not have an entry for the language in <code class="structname">pg_pltemplate</code>. But when there is an entry, the functions need not already exist; they will be automatically defined if not present in the database. (This might result in <code class="command">CREATE LANGUAGE</code> failing, if the shared library that implements the language is not available in the installation.) </p><p> In <span class="productname">PostgreSQL</span> versions before 7.3, it was necessary to declare handler functions as returning the placeholder type <code class="type">opaque</code>, rather than <code class="type">language_handler</code>. To support loading of old dump files, <code class="command">CREATE LANGUAGE</code> will accept a function declared as returning <code class="type">opaque</code>, but it will issue a notice and change the function's declared return type to <code class="type">language_handler</code>. </p></div><div class="refsect1" id="SQL-CREATELANGUAGE-EXAMPLES"><h2>Examples</h2><p> The preferred way of creating any of the standard procedural languages is just: </p><pre class="programlisting"> CREATE LANGUAGE plperl; </pre><p> </p><p> For a language not known in the <code class="structname">pg_pltemplate</code> catalog, a sequence such as this is needed: </p><pre class="programlisting"> CREATE FUNCTION plsample_call_handler() RETURNS language_handler AS '$libdir/plsample' LANGUAGE C; CREATE LANGUAGE plsample HANDLER plsample_call_handler; </pre></div><div class="refsect1" id="SQL-CREATELANGUAGE-COMPAT"><h2>Compatibility</h2><p> <code class="command">CREATE LANGUAGE</code> is a <span class="productname">PostgreSQL</span> extension. </p></div><div class="refsect1" id="id-1.9.3.70.10"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-alterlanguage.html" title="ALTER LANGUAGE"><span class="refentrytitle">ALTER LANGUAGE</span></a>, <a class="xref" href="sql-createfunction.html" title="CREATE FUNCTION"><span class="refentrytitle">CREATE FUNCTION</span></a>, <a class="xref" href="sql-droplanguage.html" title="DROP LANGUAGE"><span class="refentrytitle">DROP LANGUAGE</span></a>, <a class="xref" href="sql-grant.html" title="GRANT"><span class="refentrytitle">GRANT</span></a>, <a class="xref" href="sql-revoke.html" title="REVOKE"><span class="refentrytitle">REVOKE</span></a></span></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-createindex.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-creatematerializedview.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">CREATE INDEX </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> CREATE MATERIALIZED VIEW</td></tr></table></div></body></html>