How to enable plpython3u in PostgreSQL

Hi,

I have a database running in docker that I want to migrate to native Nixos. I can’t get the plpython3u available in PostgreSQL. From what I understand, Python should be enabled if it is available on the system (nixpkgs/pkgs/servers/sql/postgresql/generic.nix at 55d1f923c480dadce40f5231feb472e81b0bab48 · NixOS/nixpkgs · GitHub). I also saw this post and tried the override without success. I’ve enabled Python on the system with

environment.systemPackages = [ pkgs.python3 ];
sudo -u postgres /nix/store/n4gj0dmk2wpwl08s6sqng2f0jdqqkrjs-postgresql-and-plugins-17.5/bin/psql -h /tmp/tmp.sYB9hVQAgz -X -d gosk -c 'ALTER EXTENSION plpython3u UPDATE;'
WARNING:  database "gosk" has a collation version mismatch
DETAIL:  The database was created using collation version 2.35, but the operating system provides version 2.40.
HINT:  Rebuild all objects in this database that use the default collation and run ALTER DATABASE gosk REFRESH COLLATION VERSION, or build PostgreSQL with the right library version.
ERROR:  extension "plpython3u" is not available
DETAIL:  Could not open extension control file "/nix/store/gbniv0b4hm6pjky7f1iyradwdmb4g4l0-postgresql-and-plugins-15.13/share/postgresql/extension/plpython3u.control": No such file or directory.
HINT:  The extension must first be installed on the system where PostgreSQL is running.

Have you tried to use a PostgreSQL with pythonSupport explicitly enabled?

For example:

    services.postgresql = {
      package = pkgs.postgresql_16.override {
        pythonSupport = true;
        inherit (pkgs) python3;
      };
    };

With NixOS 25.05, or more specifically with postgresql: provide PL/Perl, PL/Python and PL/Tcl as packages by wolfgangwalther · Pull Request #385859 · NixOS/nixpkgs · GitHub, we moved the core procedural languages (PL/Perl, PL/Python, PL/Tcl) to packages. This has the advantage, that PostgreSQL needs to be only built once, with python support enabled - even you want to add more python dependencies. To actually use the extension, you’ll then have to enable it explicitly, though.

For NixOS, this is described in the documentation: NixOS Manual

TLDR: services.postgresql.extensions = ps: [ ps.plpython3 ];

For just Nixpkgs, without NixOS, you’ll have to use postgresql.withPackages (ps: [ ps.plpython3 ]) as your PostgreSQL package.

3 Likes

Somehow I missed this documentation entirely. I was checking out this wiki and this wiki and didn’t realize that the answer would be in the manual :face_with_hand_over_mouth:
Thank you very much for you help!

I tried, but I didn’t set the python3 attribute. I thought that it would default to python3 when not set.