{"id":16518,"date":"2021-07-02T13:44:16","date_gmt":"2021-07-02T11:44:16","guid":{"rendered":"https:\/\/www.dbi-services.com\/blog\/creating-simple-extensions-for-postgresql\/"},"modified":"2021-07-02T13:44:16","modified_gmt":"2021-07-02T11:44:16","slug":"creating-simple-extensions-for-postgresql","status":"publish","type":"post","link":"https:\/\/www.dbi-services.com\/blog\/creating-simple-extensions-for-postgresql\/","title":{"rendered":"Creating simple extensions for PostgreSQL"},"content":{"rendered":"<p>When you are using PostgreSQL you should already know that PostgreSQL comes with a <a href=\"https:\/\/www.postgresql.org\/docs\/current\/contrib.html\" target=\"_blank\" rel=\"noopener\">set of extensions by default<\/a>. It might be that you need to install an additional package if you installed PostgreSQL with a package manager to get those extensions. Usually it is called something with &#8220;contrib&#8221; in its name. There is also the <a href=\"https:\/\/pgxn.org\/\" target=\"_blank\" rel=\"noopener\">PostgreSQL Extension Network<\/a> which list a lot of external extensions that might be useful for you. If you can&#8217;t find what you&#8217;re looking for, you can still write your own extension. How to do that is the topic of this post: We&#8217;ll create a very simple extension for PostgreSQL.<\/p>\n<p><!--more--><\/p>\n<p>Lets assume you always need a table containing the list of countries with a few properties like this:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# create table countries ( id int primary key, name text, alpha2 text, alpha3 text );\nCREATE TABLE\npostgres=# copy countries \n           from program 'curl https:\/\/raw.githubusercontent.com\/stefangabos\/world_countries\/master\/data\/en\/countries.csv'\n           with (header true, delimiter ',', format csv);\nCOPY 193\n<\/pre>\n<p>This gives as 193 countries and each country has a few properties:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# select * from countries limit 5;\n id |    name     | alpha2 | alpha3 \n----+-------------+--------+--------\n  4 | Afghanistan | af     | afg\n  8 | Albania     | al     | alb\n 12 | Algeria     | dz     | dza\n 20 | Andorra     | ad     | and\n 24 | Angola      | ao     | ago\n(5 rows)\n<\/pre>\n<p>Packaging this into an extension is quite easy. You need a so called control file that provides some basic information about your extension. In my case it looks like this and I&#8217;ve placed it where all the other extensions are as well:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres@debian11:\/u01\/app\/postgres\/product\/DEV\/db_1\/share\/extension\/ [pg15] pwd\n\/u01\/app\/postgres\/product\/DEV\/db_1\/share\/extension\npostgres@debian11:\/u01\/app\/postgres\/product\/DEV\/db_1\/share\/extension\/ [pg15] cat my_cool_extension.control\n# my cool extension control file\ncomment = 'my cool extension for providing a table with countries'\ndefault_version = '1.0'\nrelocatable = false\ntrusted = true\n<\/pre>\n<p>Now we need the table and the data and this goes into a simple SQL file:<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">\npostgres@debian11:\/home\/postgres\/ [pg15] pg_dump --column-inserts --table=countries postgres &gt; \/u01\/app\/postgres\/product\/DEV\/db_1\/share\/extension\/my_cool_extension--1.0.sql\npostgres@debian11:\/home\/postgres\/ [pg15] psql -c \"drop table countries\" postgres\n<\/pre>\n<p>If you look at the default extensions they all have the first two lines of the SQL files like e.g. this:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\n-- complain if script is sourced in psql, rather than via CREATE EXTENSION\necho Use \"CREATE EXTENSION ltree_plpython2u\" to load this file. quit\n<\/pre>\n<p>This prevents that the file can directly be piped into psql and we should do the same. Only &#8220;create extension&#8221; should be used to create extensions:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres@debian11:\/home\/postgres\/ [pgdev] sed -i '1s\/^\/-- complain if script is sourced in psql, rather than via CREATE EXTENSION\/' \/u01\/app\/postgres\/product\/DEV\/db_1\/share\/extension\/my_cool_extension--1.0.sql\npostgres@debian11:\/home\/postgres\/ [pgdev] sed -i '2s\/^\/\\echo Use \"CREATE EXTENSION my_cool_extension\" to load this file. \\quitn\/' \/u01\/app\/postgres\/product\/DEV\/db_1\/share\/extension\/my_cool_extension--1.0.sql\n<\/pre>\n<p>That&#8217;s all you need to do. Having these two files in place the extension is recognized by PostgreSQL:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# select * from pg_available_extensions where comment like 'my cool%';\n       name        | default_version | installed_version |                       comment                        \n-------------------+-----------------+-------------------+------------------------------------------------------\n my_cool_extension | 1.0             |                   | my cool extension for providing a table of countries\n(1 row)\n<\/pre>\n<p>Now we can install it as usual:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# create extension my_cool_extension;\nCREATE EXTENSION\npostgres=# dx\n                                  List of installed extensions\n       Name        | Version |   Schema   |                     Description                      \n-------------------+---------+------------+------------------------------------------------------\n my_cool_extension | 1.0     | public     | my cool extension for providing a table of countries\n plpgsql           | 1.0     | pg_catalog | PL\/pgSQL procedural language\n(2 rows)\npostgres=# select * from countries limit 5;\n id |    name     | alpha2 | alpha3 \n----+-------------+--------+--------\n  4 | Afghanistan | af     | afg\n  8 | Albania     | al     | alb\n 12 | Algeria     | dz     | dza\n 20 | Andorra     | ad     | and\n 24 | Angola      | ao     | ago\n(5 rows)\n<\/pre>\n<p>Over time you might need to update your extension so lets assume in our case that there is a new country. What you need to do is create a new SQL file that does the necessary changes and update the default version in the control file:<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">\npostgres@debian11:\/u01\/app\/postgres\/product\/DEV\/db_1\/share\/extension\/ [pgdev] pwd\n\/u01\/app\/postgres\/product\/DEV\/db_1\/share\/extension\npostgres@debian11:\/u01\/app\/postgres\/product\/DEV\/db_1\/share\/extension\/ [pgdev] cat my_cool_extension--1.0--1.1.sql\n-- complain if script is sourced in psql, rather than via ALTER EXTENSION\necho Use \"ALTER EXTENSION my_cool_extension UPDATE TO '1.1'\" to load this file. quit\n-- the list of new countries\ninsert into countries (id,name,alpha2,alpha3) values (-1,'my new country','aaa','aaa');\n\npostgres@debian11:\/u01\/app\/postgres\/product\/DEV\/db_1\/share\/extension\/ [pgdev] grep default my_cool_extension.control \ndefault_version = '1.1'\n<\/pre>\n<p>Afterwards update the extension using alter extension:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">\npostgres=# select * from pg_available_extensions where name = 'my_cool_extension';\n       name        | default_version | installed_version |                       comment                        \n-------------------+-----------------+-------------------+------------------------------------------------------\n my_cool_extension | 1.1             | 1.0               | my cool extension for providing a table of countries\n(1 row)\n\npostgres=# alter extension my_cool_extension update;\nALTER EXTENSION\npostgres=# select count(*) from countries where id = -1;\n count \n-------\n     1\n(1 row)\n<\/pre>\n<p>All fine, ready to go.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you are using PostgreSQL you should already know that PostgreSQL comes with a set of extensions by default. It might be that you need to install an additional package if you installed PostgreSQL with a package manager to get those extensions. Usually it is called something with &#8220;contrib&#8221; in its name. There is also [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[229],"tags":[77],"type_dbi":[],"class_list":["post-16518","post","type-post","status-publish","format-standard","hentry","category-database-administration-monitoring","tag-postgresql"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v28.8 (Yoast SEO v28.8) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Creating simple extensions for PostgreSQL - dbi Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.dbi-services.com\/blog\/creating-simple-extensions-for-postgresql\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Creating simple extensions for PostgreSQL\" \/>\n<meta property=\"og:description\" content=\"When you are using PostgreSQL you should already know that PostgreSQL comes with a set of extensions by default. It might be that you need to install an additional package if you installed PostgreSQL with a package manager to get those extensions. Usually it is called something with &#8220;contrib&#8221; in its name. There is also [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.dbi-services.com\/blog\/creating-simple-extensions-for-postgresql\/\" \/>\n<meta property=\"og:site_name\" content=\"dbi Blog\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-02T11:44:16+00:00\" \/>\n<meta name=\"author\" content=\"Open source Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Open source Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/creating-simple-extensions-for-postgresql\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/creating-simple-extensions-for-postgresql\\\/\"},\"author\":{\"name\":\"Open source Team\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/4bae5e5dd8c7d48edbd948772b49b202\"},\"headline\":\"Creating simple extensions for PostgreSQL\",\"datePublished\":\"2021-07-02T11:44:16+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/creating-simple-extensions-for-postgresql\\\/\"},\"wordCount\":333,\"commentCount\":0,\"keywords\":[\"PostgreSQL\"],\"articleSection\":[\"Database Administration &amp; Monitoring\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/creating-simple-extensions-for-postgresql\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/creating-simple-extensions-for-postgresql\\\/\",\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/creating-simple-extensions-for-postgresql\\\/\",\"name\":\"Creating simple extensions for PostgreSQL - dbi Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#website\"},\"datePublished\":\"2021-07-02T11:44:16+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/4bae5e5dd8c7d48edbd948772b49b202\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/creating-simple-extensions-for-postgresql\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/creating-simple-extensions-for-postgresql\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/creating-simple-extensions-for-postgresql\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Creating simple extensions for PostgreSQL\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/\",\"name\":\"dbi Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/4bae5e5dd8c7d48edbd948772b49b202\",\"name\":\"Open source Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g\",\"caption\":\"Open source Team\"},\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/author\\\/open-source-team\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Creating simple extensions for PostgreSQL - dbi Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.dbi-services.com\/blog\/creating-simple-extensions-for-postgresql\/","og_locale":"en_US","og_type":"article","og_title":"Creating simple extensions for PostgreSQL","og_description":"When you are using PostgreSQL you should already know that PostgreSQL comes with a set of extensions by default. It might be that you need to install an additional package if you installed PostgreSQL with a package manager to get those extensions. Usually it is called something with &#8220;contrib&#8221; in its name. There is also [&hellip;]","og_url":"https:\/\/www.dbi-services.com\/blog\/creating-simple-extensions-for-postgresql\/","og_site_name":"dbi Blog","article_published_time":"2021-07-02T11:44:16+00:00","author":"Open source Team","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Open source Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.dbi-services.com\/blog\/creating-simple-extensions-for-postgresql\/#article","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/creating-simple-extensions-for-postgresql\/"},"author":{"name":"Open source Team","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/4bae5e5dd8c7d48edbd948772b49b202"},"headline":"Creating simple extensions for PostgreSQL","datePublished":"2021-07-02T11:44:16+00:00","mainEntityOfPage":{"@id":"https:\/\/www.dbi-services.com\/blog\/creating-simple-extensions-for-postgresql\/"},"wordCount":333,"commentCount":0,"keywords":["PostgreSQL"],"articleSection":["Database Administration &amp; Monitoring"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.dbi-services.com\/blog\/creating-simple-extensions-for-postgresql\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.dbi-services.com\/blog\/creating-simple-extensions-for-postgresql\/","url":"https:\/\/www.dbi-services.com\/blog\/creating-simple-extensions-for-postgresql\/","name":"Creating simple extensions for PostgreSQL - dbi Blog","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/#website"},"datePublished":"2021-07-02T11:44:16+00:00","author":{"@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/4bae5e5dd8c7d48edbd948772b49b202"},"breadcrumb":{"@id":"https:\/\/www.dbi-services.com\/blog\/creating-simple-extensions-for-postgresql\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.dbi-services.com\/blog\/creating-simple-extensions-for-postgresql\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.dbi-services.com\/blog\/creating-simple-extensions-for-postgresql\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/www.dbi-services.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Creating simple extensions for PostgreSQL"}]},{"@type":"WebSite","@id":"https:\/\/www.dbi-services.com\/blog\/#website","url":"https:\/\/www.dbi-services.com\/blog\/","name":"dbi Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.dbi-services.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/4bae5e5dd8c7d48edbd948772b49b202","name":"Open source Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g","caption":"Open source Team"},"url":"https:\/\/www.dbi-services.com\/blog\/author\/open-source-team\/"}]}},"_links":{"self":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/16518","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/comments?post=16518"}],"version-history":[{"count":0,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/16518\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/media?parent=16518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/categories?post=16518"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/tags?post=16518"},{"taxonomy":"type","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/type_dbi?post=16518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}