|
| 1 | +Originally from: [tweet](https://twitter.com/samokhvalov/status/1723535079451033697), [LinkedIn post](). |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +# How to install Postgres 16 with plpython3u: Recipes for macOS, Ubuntu, Debian, CentOS, Docker |
| 6 | + |
| 7 | +> I post a new PostgreSQL "howto" article every day. Join me in this |
| 8 | +> journey – [subscribe](https://twitter.com/samokhvalov/), provide feedback, share! |
| 9 | +
|
| 10 | +PL/Python is a procedural language extension for PostgreSQL that allows you to write stored procedures and triggers in |
| 11 | +Python, a widely-used, high-level, and versatile programming language. |
| 12 | + |
| 13 | +`plpython3u` is the "untrusted" version of PL/Python. This variant allows Python functions to perform operations such as |
| 14 | +file I/O, network communication, and other actions that could potentially affect the server's behavior or security. |
| 15 | + |
| 16 | +We used `plpython3u` in |
| 17 | +[Day 23: How to use OpenAI APIs right from Postgres to implement semantic search and GPT chat](0023_how_to_use_openai_apis_in_postgres.md), |
| 18 | +let's now discuss how to install it. |
| 19 | + |
| 20 | +And something tells me that we'll be using it more in the future, for various tasks. |
| 21 | + |
| 22 | +This howto is for self-managed Postgres only. |
| 23 | + |
| 24 | +## macOS (Homebrew) |
| 25 | + |
| 26 | +```bash |
| 27 | +brew tap petere/postgresql |
| 28 | +brew install petere/postgresql/postgresql@16 |
| 29 | + |
| 30 | +psql postgres \ |
| 31 | + -c 'create extension plpython3u' |
| 32 | +``` |
| 33 | + |
| 34 | +## Ubuntu 22.04 LTS or Debian 12 |
| 35 | + |
| 36 | +```bash |
| 37 | +sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" \ |
| 38 | + > /etc/apt/sources.list.d/pgdg.list' |
| 39 | + |
| 40 | +curl -fsSL https://postgresql.org/media/keys/ACCC4CF8.asc \ |
| 41 | + | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/postgresql.gpg |
| 42 | + |
| 43 | +sudo apt update |
| 44 | +sudo apt install -y \ |
| 45 | + postgresql-16 \ |
| 46 | + postgresql-contrib-16 \ |
| 47 | + postgresql-plpython3-16 |
| 48 | + |
| 49 | +sudo -u postgres psql \ |
| 50 | + -c 'create extension plpython3u' |
| 51 | +``` |
| 52 | + |
| 53 | +## CentOS Stream 9 |
| 54 | + |
| 55 | +```bash |
| 56 | +dnf module reset -y postgresql |
| 57 | +dnf module enable -y postgresql:16 |
| 58 | + |
| 59 | +dnf install -y \ |
| 60 | + postgresql-server \ |
| 61 | + postgresql \ |
| 62 | + postgresql-contrib \ |
| 63 | + postgresql-plpython3 |
| 64 | + |
| 65 | +postgresql-setup --initdb |
| 66 | + |
| 67 | +systemctl enable --now postgresql |
| 68 | + |
| 69 | +sudo -u postgres psql \ |
| 70 | + -c 'create extension plpython3u' |
| 71 | +``` |
| 72 | + |
| 73 | +## Docker |
| 74 | + |
| 75 | +```bash |
| 76 | +echo "FROM postgres:16 |
| 77 | +RUN apt update |
| 78 | +RUN apt install -y postgresql-plpython3-16" \ |
| 79 | +> postgres_plpython3u.Dockerfile |
| 80 | + |
| 81 | +sudo docker build \ |
| 82 | + -t postgres-plpython3u:16 \ |
| 83 | + -f postgres_plpython3u.Dockerfile \ |
| 84 | + . |
| 85 | + |
| 86 | +sudo docker run \ |
| 87 | + --detach \ |
| 88 | + --name pg16 \ |
| 89 | + -e POSTGRES_PASSWORD=secret \ |
| 90 | + -v $(echo ~)/pgdata:/var/lib/postgresql/data \ |
| 91 | + postgres-plpython3u:16 |
| 92 | + |
| 93 | +sudo docker exec -it pg16 \ |
| 94 | + psql -U postgres -c 'create extension plpython3u' |
| 95 | +``` |
0 commit comments