Install plpython on mac with python 2.7
Asked Answered
B

6

13

I need to install plpython because I'm getting the error

could not access file "$libdir/plpython2": No such file or directory

when trying to do python manage.py migrate. I have seen different suggestions on how to install this package but none works for me because I need to be using python version 2.7 (some people suggested installing python 3.2) and I cannot run sudo apt-get install ... because I have to be working on a mac.

I have tried running

CREATE LANGUAGE plpython2u;

but I got the error

ERROR:  could not access file "$libdir/plpython2": No such file or directory

Also, I have tried pip/brew install plpython. But no result. Any suggestions?

Butterandeggs answered 27/6, 2016 at 20:18 Comment(0)
B
13

If anybody has the same problem, I fixed this by uninstalling postgres and installing it using brew install postgres --with-python

Butterandeggs answered 27/7, 2016 at 17:53 Comment(4)
i did this and got plpython3u and not plpythonu, neither plpython2uHydrostatics
Looked a lot before finding this.Consolata
Unfortunately it does not work for me #53975651Consolata
This used to work brew install [email protected] --with-python@2 however now returns the following error: "Warning: [email protected]: this formula has no --with-python@2 option so it will be ignored!". The formula page even lists this option still... formulae.brew.sh/formula/[email protected]. Looks like it has been removed for some reason...Vane
D
4

I quickly create a tap formula for the latest postgresql (11.2), add a --with-python option to compile options. So far it's work. No binary version, only compile from source. To use it:

brew tap indlin/postgresql-py
brew install postgresql-py

If I understand correctly, this is the official path for change options in brew (create your own tap formula). I don't understand why they do it???

Denounce answered 20/2, 2019 at 14:22 Comment(0)
S
3

(macOS Catalina 10.15.5, Homebrew 2.3.0)

The voted solution does not work with recent versions of Homebrew, which does not support options anymore, see https://github.com/Homebrew/homebrew-core/issues/31510.

> brew reinstall [email protected] --with-python
...
Error: invalid option: --with-python

Fortunately, there is petere / homebrew-postgresql

I first updated the XCode command tools, in order to avoid this error:

configure: error: header file <perl.h> is required for Perl

with

> sudo rm -rf /Library/Developer/CommandLineTools
> sudo xcode-select --install

And then

> brew tap petere/postgresql
> brew reinstall petere/postgresql/[email protected]

brought plpython2 back to life for me.

Spillway answered 1/6, 2020 at 14:48 Comment(0)
V
2

Ugh, this was a real pain as they have removed the with-python@2 option. I managed to get it installed with the following steps:

git clone https://github.com/Homebrew/homebrew-core.git
cd homebrew-core/
git checkout c2c0659f5a2e5be9c54c214e5aa19a2fe2cdc374
brew install --build-from-source ./Formula/[email protected] --with-python@2
brew services restart [email protected]

Probably there is a better way but this worked for me.

Vane answered 28/1, 2019 at 2:8 Comment(0)
V
1

Many thanks to @facetoe; sadly as others have said this commit took out the functionality https://github.com/Homebrew/homebrew-core/commit/c55743ce2e993d3407a7f7932abfe4910a25f953#diff-c290cd53a44f82f9ba1f4d59d9f90140. Ultimately i want the latest just with python; the quickest way for me was:

git clone https://github.com/Homebrew/homebrew-core.git
cd homebrew-core/
vi Formula/postgresql.rb 

here you need to add into the install block the environment variable for python and --with-python into the args listing. My diff on current master is:

@@ -38,6 +38,7 @@ class Postgresql < Formula
   def install
     ENV.prepend "LDFLAGS", "-L#{Formula["[email protected]"].opt_lib} -L#{Formula["readline"].opt_lib}"
     ENV.prepend "CPPFLAGS", "-I#{Formula["[email protected]"].opt_include} -I#{Formula["readline"].opt_include}"
+    ENV["PYTHON"] = which("python3")
 
     args = %W[
       --disable-debug
@@ -56,6 +57,7 @@ class Postgresql < Formula
       --with-libxslt
       --with-openssl
       --with-pam
+      --with-python
       --with-perl
       --with-tcl
       --with-uuid=e2fs

Then finally you want to install it with:

brew install --build-from-source ./Formula/postgresql.rb
sudo install_name_tool -change @rpath/Python3.framework/Versions/3.8/Python3 /usr/local/Cellar/[email protected]/3.8.5/Frameworks/Python.framework/Versions/3.8/Python /usr/local/lib/postgresql/plpython3.so
brew services start postgresql
Vehemence answered 2/10, 2020 at 9:23 Comment(0)
S
0

Working with this in several environments I found it's easier to run postgres in a Docker container using a modified postgres image. What follows are simple steps for doing that.

First install Docker.

Second create a directory to contain your dB and open a terminal in that directory.

Third build the desired image. Create a Dockerfile that tells Docker what the image should look like. This is what I use to specify that I want a container running a postgres db and to include python3 as a procedural language extension (though you could specify python2). Note that I specify the version of postgres I want (13) and the compatible plpython3 (also 13). I also include a reference to a 'requirements' file (see next step):

FROM postgres:13

RUN apt-get update
RUN apt-get -y install python3 postgresql-plpython3-13
RUN apt-get -y install python3-pip

RUN  apt-get clean && \
     rm -rf /var/cache/apt/* /var/lib/apt/lists/*

# Requirements installation
COPY requirements_dockerbuild13.txt /tmp/
RUN python3 -m pip install --upgrade pip
RUN python3 -m pip install --requirement /tmp/requirements_dockerbuild13.txt

Fourth, is an optional step to specify any additional libraries/packages/modules of interest. Those can be put in a 'requirements.txt' file. If you don't have any requirements just delete the line referring to them in the Dockerfile. If you do have requirements then create a 'requirements.txt' file. In my case I wanted the image to also include 'pandas' and 'networkx'. My file looks like (yes, two lines of text):

pandas>=1.3.5
networkx>=2.0

Fifth, at the command line in your terminal run docker build -t xxx .. The -t xxx is the name you give to the image and the . means the Dockerfile is in the current directory. Voila you've now have a postgres image named 'xxx' that contains postgres/python3/plpython3/pandas/networkx In my case the tag I used was 'postgresql-plpython3-13'.

To use the image you launch it using docker-compose up -d and that command will launch an image in accordance with the docker-compose.yml located in the directory. Yes, one more text file to create. Mine looked like this:

version: "3"
services:
  postgres:
    image: postgresql-plpython3-13 <--- name of the image just created
    container_name: cain_db  <--- arbitrary name of container running image 
    command: ["-c", "logging_collector=on","-c", "log_statement=all"]
    volumes:
      - ./caindata:/var/lib/postgresql/data <--- *comments below*
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=cain_data  <---- name of db
    ports: 
      - 5566:5432  <--- making the db available on port 5566 instead of 5432
volumes:
  caindata:  <--- name of the volume
  

That docker-compose file makes docker run a container named cain_db with the postgres image you created running inside it. That db can be connected to on port 5566 a port I chose to be different from the default postgres port (I could have used 5432). The db will be named 'cain_data' and here's the important bit: the db will be in a folder in the current directory named 'caindata' as opposed to /var/lib/postgresql/data which is where MacOS would put it.

To shut down the container use docker-compose down --remove-orphans. The database will persist and when you launch docker-compose up -d everything will be as it was when you shut down.

Recap:

  1. build the desired image with or without requirements file
  2. compose the appropriate docker-compose.yml file
  3. launch db with docker-compose up, shut down with docker-compose down

Additional notes:

  • If you follow all the steps you'll have a directory with: a file called Dockerfile, a file called requirements.txt, a file called docker-compose.yml, and a sub-directory with the db.
  • Neat thing, add a 'readme' file and zip the directory. Now you can provide someone else who has Docker with your complete db.
Sybilla answered 27/5, 2022 at 14:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.