|
| 1 | +A Postgres [trusted lanuguage extension](https://github.com/aws/pg_tle) (TLE) consists of the following files: |
| 2 | + |
| 3 | +1. Script files. These files contain the SQL commands to create the extension's objects. |
| 4 | +2. Control files. These files contain basic properties of the extension itself. |
| 5 | + |
| 6 | +For an extension to be valid, one file of each type must be present in an extension. For example, if you want to create an extension named `my-extension`, create the following folder structure: |
| 7 | + |
| 8 | +- my-extension |
| 9 | + - my-extension.control |
| 10 | + - my-extension--0.0.1.sql |
| 11 | + - README.md |
| 12 | + |
| 13 | +In the above example, the `my-extension` folder contains the extension files. Names of the files are important. The control file should be named `<extension_name>.control` and the script file should be named `<extension_name>--<extension_version>.sql`. The readme file should be named `README.md` (case sensitive). |
| 14 | + |
| 15 | +## Control Files |
| 16 | + |
| 17 | +A control file contains metadata about the extension in key-value pairs. The most common keys that you should consider setting are the following: |
| 18 | + |
| 19 | +- default_version (string). The version to use if the user doesn't provide one in the `create extension` command. |
| 20 | +- comment (string). A comment added to the extension object created in the database. |
| 21 | +- requires (string). A comma separated list of extensions that this extension depends on. |
| 22 | +- relocatable (boolean). Set to true if the extension's objects can be moved to a different schema after they are created. |
| 23 | +- superuser (boolean). Set to true if only superusers should be able to create this extension. |
| 24 | + |
| 25 | +For example, the [pgjwt extension's control file](https://github.com/michelp/pgjwt/blob/master/pgjwt.control) looks like this: |
| 26 | + |
| 27 | +```control |
| 28 | +# pgjwt extension |
| 29 | +comment = 'JSON Web Token API for Postgresql' |
| 30 | +default_version = '0.2.0' |
| 31 | +relocatable = false |
| 32 | +requires = pgcrypto |
| 33 | +superuser = false |
| 34 | +``` |
| 35 | + |
| 36 | +For a complete list of keys available in a control file, refer to [Postgres documentation](https://www.postgresql.org/docs/current/extend-extensions.html#EXTEND-EXTENSIONS-FILES). |
| 37 | + |
| 38 | +### Syntax |
| 39 | + |
| 40 | +A control file contains key-value pairs. Each pair should be on a separate line. Empty lines are ignored. Text after a `#` is a comment and is also ignored. Keys should start with a letter and contain only letters or digits. The `=` sign following a key is optional, but there must be at least one whitespace after a key if `=` is omitted. Values can be either a boolean or a string. Valid values for a boolean are `true` or `false`. Strings are anything between a pair of single quotes. If you want to include a single quote in the middle of a string, use `''` to escape it. A complete list of escape sequences is: |
| 41 | + |
| 42 | +1. `\b` - backspace |
| 43 | +2. `f` - formfeed |
| 44 | +3. `\n` - newline |
| 45 | +4. `\r` - carriage return |
| 46 | +5. `\t` - tab |
| 47 | +6. `''` - single quote |
| 48 | + |
| 49 | +Strings which do not contain whitespace or escape sequences can be written without the surrounding single quotes. |
| 50 | + |
| 51 | +Some examples of valid control file syntax follow: |
| 52 | + |
| 53 | +``` |
| 54 | +# A boolean value |
| 55 | +relocatable = true |
| 56 | +
|
| 57 | +# `=` is optional |
| 58 | +superuser false |
| 59 | +
|
| 60 | +# A string |
| 61 | +comment = 'a comment for the extension' |
| 62 | +
|
| 63 | +# A string without quotes should not have whitespace or escape sequences |
| 64 | +comment = ACommentForTheExtension |
| 65 | +
|
| 66 | +# A string with escapse sequences |
| 67 | +comment = 'A double quote '' and a newline \n' |
| 68 | +``` |
| 69 | + |
| 70 | + |
| 71 | +## Script Files |
| 72 | + |
| 73 | +Script files contain the SQL commands to create or modify database objects. These database objects can be, but are not limited to, tables, views, functions, types, operators etc. For example, the [pgjwt's `pgjwt--0.1.1.sql` file](https://github.com/michelp/pgjwt/blob/master/pgjwt--0.1.1.sql) contains definitions for functions which the extension adds to the database. One exception to the SQL commands which can exist in a script file are transaction control commands like `BEGIN`, `COMMIT`, `ROLLBACK` etc. |
| 74 | + |
| 75 | +You might have noticed a strange line at the beginning of the [`pgjwt-0.1.1.sql` file](https://github.com/michelp/pgjwt/blob/master/pgjwt--0.1.1.sql) starting with `\echo`. This line prevents the script file from being run accidentally in `psql`. Lines starting with `\echo` are run only in `psql` but are ignored when the script file is executed by the [`CREATE EXTENSION` command](https://www.postgresql.org/docs/current/sql-createextension.html). It is recommended that you include such a line at the beginning of your script file. |
| 76 | + |
| 77 | +### Update Scripts |
| 78 | + |
| 79 | +Update scripts are used to update an installed extension. An update script should be named `<extension_name>--<old_version>--<new_version>.sql`. For example if version `1.0` of `my-extension` was published already and you want to publish a new version `1.1` you need to create `my-extension--1.0--1.1.sql`. Update scripts can create new database object or modify/delete existing objects created by the previous version of the extension. |
| 80 | + |
| 81 | +## README.md File |
| 82 | + |
| 83 | +README.md file contains documentation for the extension in the [markdown](https://en.wikipedia.org/wiki/Markdown) format. The documentation typically contains installation and usage instructions about the extension. |
0 commit comments