S
S
Semion1202020-12-09 12:35:17
PostgreSQL
Semion120, 2020-12-09 12:35:17

Postgresql not working with Django? Can't install psycopg2?

Input:
1) Machine: Mac OS Mojave 10.14.6
2) Python latest version
3) Django latest version
4) Postgresql latest version.
5) psycopg2 is the latest version, but it doesn't matter in my case.

Objective:
Interview test: write a small Django project using Postgresql.

Problem:
I installed Django with a virtual environment without problems. Postgresql itself works and installed without problems. I was able to create the user and db without error.

I tried to install psycopg2 and it gave me an error on lines 400-500. Ok, I was not at a loss and found an option in the official documentation: install the binary version. Ok, installed, everything is ok.
(I tried to install via pip, brew, easy-install)

Then I changed the default settings in settings.py to:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'labmedia_project',
        'USER': 'labmedia_user',
        'PASSWORD': 'qweasd',
        'HOST': 'localhost',
        'PORT': 5432,
    }
}

Fine. I wrote two models, I apply makemigrations and it writes to me that no changes were found.

Then began a series of trial and error. I visited a total of 30+ sites, including foreign ones, with the hope of finding an answer. None of the suggestions helped me: I changed the paths, tried to install via setup.py, and so on.

Perhaps the problem lies in the fact that most manuals write solutions for Linux. But even where they write for MacOS, it didn’t help either.

Regarding the binary file: everything is fine, but it does not work by itself. The installation is described on the official website , but a piece of code is written there:

import psycopg

# Connect to your postgres DB
conn = psycopg.connect("dbname=test user=postgres")

# Open a cursor to perform database operations
cur = conn.cursor()

# Execute a query
cur.execute("SELECT * FROM my_data")

# Retrieve query results
records = cur.fetchall()


Of course, nowhere is it written where to insert it. Therefore, I went to Google, but no one describes how to use it, apparently everyone understands everything.

Questions:
1) How can I fix the error? All the same, I want to have the full version.
2) How to properly use a binary file? With a normal installation of either, it simply does not work.

PS Screenshot and error log: here

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
alternativshik, 2020-12-09
@alternativshik

pip install psycopg2-binary and everything works out of the box. The fact that no changes were found during migration is beside the point. You either didn’t add your application to settings, or you had to directly specify in which application to look for changes.

S
Sergey Tikhonov, 2020-12-09
@tumbler

Your question will not help anyone else if you do not provide an error in the request text.
ld: library not found for -lssl
Actually, everything. Install the libssl dev package.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question