Python 与 PostgreSQL
Table of Contents
1 连接到 PostgreSQL 的问题
首先执行 pip install psycopg2
安装驱动包。如果缺少 pg_config
文件会出现以下报错。
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 ...
pg_config
在 postgresql-devel
这个包里,需要安装:
- Debian 系:
sudo apt install libpq-dev
- Redhat 系:
sudo dnf install libpq-devel
如果没有安装 gcc 也会报错:
... unable to execute 'gcc': No such file or directory It appears you are missing some prerequisite to build the package from source. You may install a binary package by installing 'psycopg2-binary' from PyPI. If you want to install psycopg2 from source, please install the packages required for the build and try again. ...
sudo dnf install gcc
或者 sudo apt install gcc
安装 gcc 就好了。