Error trying to install Postgres for python (psycopg2)

The python-dev package is required for compilation of Python extensions written in C or C++, like psycopg2. If you’re running a Debian-based distribution (e.g. Ubuntu), you can install python-dev by running sudo apt install python-dev or sudo apt install python3-dev depending on your python version (for example, sudo apt install python3.8-dev). After that, proceed to … Read more

Error: pg_config executable not found when installing psycopg2 on Alpine in Docker

Tested with Python 3.4.8, 3.5.5, 3.6.5 and 2.7.14 (just replace 3 with 2): # You can use a specific version too, like python:3.6.5-alpine3.7 FROM python:3-alpine WORKDIR /usr/src/app COPY requirements.txt . RUN \ apk add –no-cache postgresql-libs && \ apk add –no-cache –virtual .build-deps gcc musl-dev postgresql-dev && \ python3 -m pip install -r requirements.txt –no-cache-dir … Read more

Python/psycopg2 WHERE IN statement

For the IN operator, you want a tuple instead of list, and remove parentheses from the SQL string. # using psycopg2 data=(‘UK’,’France’) sql=”SELECT * from countries WHERE country IN %s” cur.execute(sql,(data,)) During debugging you can check that the SQL is built correctly with cur.mogrify(sql, (data,))

Mac + virtualenv + pip + postgresql = Error: pg_config executable not found

On the Mac, if you’re using Postgres.app, the pg_config file is in your /Applications/Postgres.app/Contents/Versions/<current_version>/bin directory. That’ll need to be added to your system path to fix this error, like this: export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/<current_version>/bin So for example, if the current Postgres.app version is 9.5, this export line would be: export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.5/bin With more recent versions of the … Read more

How to set up a PostgreSQL database in Django

You need to install psycopg2 Python library. Installation Download http://initd.org/psycopg/, then install it under Python PATH After downloading, easily extract the tarball and: $ python setup.py install Or if you wish, install it by either easy_install or pip. (I prefer to use pip over easy_install for no reason.) $ easy_install psycopg2 $ pip install psycopg2 … Read more

Python/postgres/psycopg2: getting ID of row just inserted

cursor.execute(“INSERT INTO …. RETURNING id”) id_of_new_row = cursor.fetchone()[0] And please do not build SQL strings containing values manually. You can (and should!) pass values separately, making it unnecessary to escape and SQL injection impossible: sql_string = “INSERT INTO domes_hundred (name,name_slug,status) VALUES (%s,%s,%s) RETURNING id;” cursor.execute(sql_string, (hundred_name, hundred_slug, status)) hundred = cursor.fetchone()[0] See the psycopg docs … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)