1

I made clean install of Django v1.11.10 now. When I run python manage.py runserver everything works fine. But when I try connect to Postgres database, I install package pip install psycopg2, modify DATABASES varibale and after running runserver command it fails with Illegal instruction error:

Performing system checks...

System check identified no issues (0 silenced).
Illegal instruction: 4

What is it? How to get log error? I use Mac OS 10.11.6, PostgresApp (tried on v9 and v10 server to check error source). Python 3.6.4 (via virtualenv).

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'mydb',
        'USER': 'sirjay',
        'PASSWORD': '',
        'HOST': 'localhost',
        'PORT': '',
    }
}

Error is the same if I set NAME or USER incorrect or even if I turn off Postgres.app server. It's like Django does not see Postgres. But with phpPgAdmin I can connect to Postgres server.

sirjay
  • 1,767
  • 3
  • 32
  • 52
  • Added `DATABASES` – sirjay Feb 09 '18 at 16:33
  • I don't understand, can you explain more clear please? Note: if I turn off Postgres.app server, error is the same. – sirjay Feb 09 '18 at 16:43
  • @A.B. I have set that fields not empty and error is the same. It's like Django does not see Postgres. But. With `phpPgAdmin` I can connect to Postgres server. – sirjay Feb 09 '18 at 16:50
  • 3
    Does using the [`psycopg2` module](http://initd.org/psycopg/docs/usage.html#basic-module-usage) alone still cause the error (it should)? If so, try uninstalling `psycopg2` and reinstalling it from source instead of using a binary wheel: `pip install --no-binary psycopg2 psycopg2`. – Blender Feb 09 '18 at 16:57
  • @Blender This helped!! So, how do you think what the hell was that? Who `--no-binary` helped? – sirjay Feb 09 '18 at 17:03
  • 1
    @sirjay: psycopg2 has some parts written in C and it seems like your Mac's CPU is too old for the pre-compiled binary on PyPI (*Illegal instruction*). Compiling it yourself will guarantee it'll work on your CPU. – Blender Feb 09 '18 at 17:06

3 Answers3

11

psycopg2 is partly written in C and needs to be compiled. When you pip install a package, there's often a pre-compiled binary wheel available for download.

For some reason, the pre-compiled psycopg2 module contains instructions that your CPU can't recognize (probably because your processor is too old). You can fix this by compiling the module yourself, which will ensure the code works on your CPU:

$ pip install --no-binary psycopg2 psycopg2

--no-binary psycopg2 is a separate option so you'll have to specify the package name twice. You can include this in your requirements.txt as well:

psycopg2==a.b.c    --no-binary psycopg2
Blender
  • 289,723
  • 53
  • 439
  • 496
2

Blender's answer and this other question pointed to the problem, but when running the suggested command, I was getting the following error:

 Error: pg_config executable not found.

    pg_config is required to build psycopg2 from source.  Please add the directory
    containing pg_config to the $PATH or specify the full executable path with the
    option:

        python setup.py build_ext --pg-config /path/to/pg_config build ...

    or with the pg_config option in 'setup.cfg'.

As I have no clue about settings, configurations and so on, it took a while to find out how to do it. Here it is in case somebody else faces the same issue:

pip uninstall psycopg2

then

export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/latest/bin/

then

pip install --no-binary :all: psycopg2

This solved the problem for me.

J0ANMM
  • 7,849
  • 10
  • 56
  • 90
0

Reinstalling with pip install --no-binary psycopg2 psycopg2 solved problem

sirjay
  • 1,767
  • 3
  • 32
  • 52