The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Add TLE blog post.
Michel Pelletier25c69739ee48
diff --git a/apps/www/_blog/2023-04-14-pg-tle.mdx b/apps/www/_blog/2023-04-14-pg-tle.mdxnew file mode 100644index 00000000000..8c2c270824e--- /dev/null+++ b/apps/www/_blog/2023-04-14-pg-tle.mdx@@ -0,0 +1,66 @@+---+title: 'User-managed Trusted Language Extensions for Supabase and RDS'+launchweek: 7+tags:+ - launch-week+ - tle+date: '2023-04-14'+to_depth: 3+author: michelp,oli+image: launch-week-7/day-2-edge-runtime/edge-runtime-og.png+thumb: launch-week-7/day-2-edge-runtime/edge-runtime-thumb.jpeg+---++# User-managed Trusted Language Extensions for Supabase and RDS++## Summary++Most PostgreSQL cloud providers, such as Supabase and AWS RDS, do not allow users to install Postgres extensions into their databases until they have been rigorously vetted. Because of this, many useful Postgres extensions cannot be used on cloud platforms. However, the RDS has developed a new kind of extension called a **Trusted Language Extension** has been introduced that allows extensions written in Trusted Languages to be created from any database user without requiring filesystem access.++# Introduction++Before exploring what a Trusted Language Extension is, let’s cover the basics of Postgres Extensions and the notion of “Trusted Languages” in general.++A Postgres Extension is a bundle of SQL and other code, primarily [Data Definition Language](https://www.postgresql.org/docs/current/ddl.html) (DDL) code installed into the server’s filesystem, and instantiated in a database with the `[CREATE EXTENSION](https://www.postgresql.org/docs/current/sql-createextension.html)` command. This code can contain any SQL, but almost always includes additional function objects created with the `[CREATE FUNCTION](https://www.postgresql.org/docs/current/sql-createfunction.html)` DDL statement that expose the core features of the extension to the database in some programming language, including an external library function written in C.++Extensions provide a wide variety of additional functionality to Postgres and allow third party feature distribution, like new data types, storage engines, helper functions, even entire language runtimes like Javascript or Python, without requiring changes to the Postgres database itself.++Some examples of popular Postgres extensions include:++- [PostGIS](https://postgis.net/): for working with geospatial data+- [Timescale DB](https://www.timescale.com/): for working with timeseries data+- [Citus](https://www.citusdata.com/): for working with distributed tables+- [plv8](https://plv8.github.io/): for running Node.js inside Postgres+- [pgsodium](https://github.com/michelp/pgsodium): for accessing the libsodium encryption API++Before extensions can be created, they must first be installed on the filesystem of the database server, which is an important security measure because extensions in some cases may require external libraries and can interact with the internals of Postgres. Their installation is something that requires careful consideration.++## Trusted vs Untrusted++As mentioned, extensions often include function objects created with `CREATE FUNCTION`. Many extensions provide functions whose implementation is written in C, and creating them in a database means that the compiled C code is “dynamically linked” into your running Postgres process. These dynamically-loaded libraries can now access every aspect of your running database process, right down to raw memory. They are essentially database superusers on steroids. Because of this, C is an “untrusted language” and installing extensions written in C requires filesystem access.++However there are several *trusted* languages in Postgres that don’t provide raw access to process memory, the filesytem, or other system functions. These languages obey the security rules of Postgres. The most obvious example of these trusted languages is SQL itself. Another built-in trusted language in Postgres is called [pl/pgsql](https://www.postgresql.org/docs/current/plpgsql.html). It looks a lot like SQL but with some imperative programming features like `IF` blocks and `FOR` loops. The aforementioned plv8 is also a trusted language, as are the languages [pl/perl](https://www.postgresql.org/docs/current/plperl.html) and [pl/tcl](https://www.postgresql.org/docs/current/pltcl.html). Other languages like [pl/python](https://www.postgresql.org/docs/current/plpython.html) are ***not*** trusted because of the way they are integrated into Postgres, they do not obey the “rules” of Postgres security, and so are not included in most cloud provider platforms since they would allow database users to effectively act as a superuser.++## Trusted Language Extensions++Near the end of 2022, we got a surprise gift from the team at Amazon Web Services, a new Postgres extension called `pg_tle`, that allows you to **Trusted Language Extensions (TLEs).** A TLE is just like a regular Postgres extension, except that **does not need to be installed on the server filesystem** it is written in a **trusted language** you can install extensions from any database client using the function `pgtle.install_extension()`, just like you can create other objects like tables and views.++Of course, TLEs *must* be written in trusted languages as described above, hence the name, so no extensions that are written in C can be TLEs. There are still many extensions that are already written in trusted languages, and soon many more to come with some exciting progress in more performant trusted languages like [pl/rust](https://github.com/tcdi/plrust) which recently reached an official 1.0.0 version release. By removing the constraint that extensions must be installed on the server filesystem, TLEs open up a whole new world of possibilities for packaging and distributing new and modular functionality for Postgres.++pg_tle is available right now on new Supabase projects, and on Amazon RDS for Postgres 14.5 and higher. At Supabase we also include an extension called [pgsql-http](https://github.com/pramsey/pgsql-http), which lets you make HTTP calls from SQL to REST APIs. So, naturally putting two and two together, we thought it would be cool to see if it’s possible to install TLEs from a REST API using only SQL!++You can, and we’ll show you how downloading TLEs directly into your database turns out not only to be possible but also very powerful! In this blog post we will present a prototype for this idea, and our future plans to extend TLEs into a full *community ecosystem* of installable packages, just like many other languages enjoy. pip for Python, npm for Javascript, cargo for Rust, cpan for Perl, and now dbdev for Postgres!++## The Extension Shortfall++Because untrusted extensions are, by nature, able to break the security rules of Postgres, cloud providers are quite conservative about what extensions to provide to their customers. New extensions are rarely adopted quickly. This slows down the speed that new software can be created and shared. Open source requires opportunity and momentum, and by closing off the opportunity for Postgres users to quickly create, install, evaluate and iterate extensions, progress is slowed. TLEs offer an opportunity for open source developers to create and share code without having to wait for cloud providers or other gatekeepers to install it.++After AWS released `pg_tle`, we realized immediately how powerful the tool is, and how it will change the way people release and install trusted extensions forever. After reaching out, we started collaborating with the TLE team at AWS and exchanging ideas and resources to further promote TLEs in both our database platform and theirs. By both Supabase and AWS providing the TLE extension in their respective Postgres platforms, we are unifying a standard package platform across two of the largest Postgres cloud providers in the world. We hope that other Postgres cloud providers will get on the bandwagon and provide TLE support as well, so that extension developers can leverage a large community base that greatly multiplies the reach of their efforts.++## Installing dbdev with Migrations++If you don’t have a Supabase account head on over to our [signup page](https://app.supabase.com/sign-up) for a free hosted Postgres database that’s ready to go!++Next you will need to install the dbdev client in your database. You can achieve that by executing the snippet of SQL code in the [link here](https://database.dev/installer). Typically you would include this snippet of SQL code in your migration tooling so that it is available whenever you bootstrap your projects. The setup script is designed to be “idempotent” so you can run it multiple times without harm. Once the client is installed from your migration scripts, you can call `dbdev.install(<package_name>)` to install the actual TLEs you want to use in your application. Whenever you want to add a new TLE, add a new migration that calls `dbdev.install()`, and you’re ready to go! You can discover packages to install at [database.dev](https://www.notion.so/LW7-dbdev-database-dev-read-only-b537cf0c90934f2280f23898720e8b3e).++If you are using RDS, TLEs are available immediately but the `dbdev` client is not yet installable due to its dependency on `pgsql-http`. We are currently working with the RDS team to find a secure option for package fetching that removes all non-`pg_tle` dependencies with the goal of enabling `dbdev` on RDS and any other Postgres hosting provider with support for `pg_tle`.\ No newline at end of file