These instructions explain how to install PL/Rust on a typical OS PostgreSQL
installation installed using the OS' package manager. These instructions
include steps for trusted and untrusted
plrust and are tested using Ubuntu 22.04 and PostgreSQL 15.
PostgreSQL 15 for this document is installed using
See the PostgreSQL apt wiki page
Steps to install PL/Rust:
- Install Rust
- Install pgrx
- Install PL/Rust
- Create amazing things!
PL/Rust requires PostgreSQL and all prerequisites outlined for pgrx are installed.
PL/Rust also requires that any databases in which it's created is
UTF8. Postgres' default encoding is determined
by the locale of the environment when
initdb is first run. Depending on your operating system configuration, this may
not resolve to
Building PL/Rust from source requires
cargo-pgrx which requires a development toolchain capable of building Postgres itself.
Installing PL/Rust with these instructions installs
plrust as the Linux
postgres user. The
is created during the standard PostgreSQL installation via
pgrx to successfully install
user needs ownership of the
The standard Ubuntu locations are indicated below.
sudo chown postgres -R /usr/share/postgresql/15/extension/
sudo chown postgres -R /usr/lib/postgresql/15/lib/
These permissions are later reset back to being owned by
in the Reset Permissions section.
Installing PL/Rust requires that the
rustc compiler is available
to the user installing it.
Switch to the
postgres Linux user and change into its home directory.
sudo su - postgres
The typically installation for
If you want to install
rustc without using
rustup see the
Other Rust installation methods
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
rustup installer prompts for an installation choice. The
default installation (1) should work for most use cases.
1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
After installing rust, use
exit to log out and back in to the
account. This ensures your terminal is using the newly installed
# Log in as postgres fresh with rustc installed
sudo su - postgres
PL/Rust is installed from source code using pgrx. This installation
requires that pgrx is compiled using a specific version of
rustc version required for PL/Rust is defined in the project's
The steps below ensure the proper versions are used.
plrust repo from GitHub and change into the
rustc -V in this location is used to verify
the version reported is by
rustc -V is the version defined by PL/Rust.
git clone https://github.com/tcdi/plrust.git
The output from
rustc -V should look similar to the following example.
rustc 1.72.0 (90c541806 2023-05-31)
rustup default to check that the explicit version of
You need to see the version number reported in by
rustc -V in
rustup default output.
The expected output is below.
rustup default returns a different version number or
set the default version as shown below and check that the output
rustup default 1.72.0
rustccannot be used to install Trusted PL/Rust. This is the case even when the
stableversion is identical to the tagged version number, such as
The above checks of
rustc -V and
rustup default are important to
follow before installing pgrx and PL/Rust.
You must install
pgrx with the version of
rust-toolchain.toml. Failing to do so will result in a
mismatched version error in a subsequent step.
rustup default results in
errors when creating functions with trusted PL/Rust. The error can
manifest as a problem in the
postgrestd linking with the following error.
This happens because Rust makes a distinction between the latest stable
version of Rust, and the actual version of the stable release (e.g. 1.72.0),
even when they refer to the same release.
Error loading target specification: Could not find specification for target "x86_64-postgres-linux-gnu".
The PL/Rust extension is built and installed
Install pgrx with the
--locked option. This step takes a few
cargo install cargo-pgrx --locked
Pgrx needs to be initialized for use with the PostgreSQL installation.
This is done using
pgrx init. This step needs to know where your
pg_config file is located at. If you have a standard Ubuntu
apt installation of PostgreSQL with a single version of PostgreSQL
installed you can use the generic
cargo pgrx init --pg15 /usr/bin/pg_config
cargo pgrx init looks like the following example.
You may notice it mentions information about a new data directory under your
~/.pgrx/ directory. This does not replace your PostgreSQL instance's
data directory. The
~/.pgrx/data-15/ directory is there in case you run
cargo pgrx run pg15, which would use this custom data directory, not your installation's data directory.
Initializing data directory at /var/lib/postgresql/.pgrx/data-15
pg_config used above will not work
for all installations, such as if you have both PostgreSQL 14 and 15
installed on one instance.
In these cases you should specify the exact
file for your installation.
cargo pgrx init --pg14 /usr/lib/postgresql/14/bin/pg_config
The instructions on this page have setup the prerequisite software required to install PL/Rust. The next section, Install PL/Rust, finishes the installation process.