- 環境:MacOS 10.12.6
開始說正題。
Redshift是基於PostgreSQL的二次開發應用,所以,能連接PostgreSQL的工具都可以用來連接Redshift。我選擇的是使用最廣泛的psycopg2
。
如果上來就執行:
$ pip install psycopg2
你會看到下面的提示錯誤:
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'.
If you prefer to avoid building psycopg2 from source, please install the PyPI
'psycopg2-binary' package instead.
For further information please check the 'doc/src/install.rst' file (also at
<http://initd.org/psycopg/docs/install.html>).
----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
提示需要一個叫做pg_config
的東西。pg_config
是個編譯PostgreSQL源碼後得到的一個文件。所以想要通過這種方式安裝psycopg2
就需要手動去編譯源碼。
如果懶得編譯,人家已經替你想好了辦法,正如提示裏所言,
If you prefer to avoid building psycopg2 from source, please install the PyPI
'psycopg2-binary' package instead.
因此,通過下面命令安裝刪減版的psycopg2
,
$ pip install psycopg2-binary
然後,在本機上就可以正常使用了。
但是,Lambda不可以。不同於獨立的機器,Lambda需要完整的依賴包才能執行。
在Github上搜到了awslambda-psycopg2,作者介紹說是專門解決在Lambda上使用psycopg2
的。
按照README.md
的步驟,先編譯PostgreSQL,再用生成的pg_config
去編譯psycopg2
。但執行後總會報一個錯誤,
No module named 'psycopg2._psycopg'
看着issue裏面幾個老外說來說去,也沒說出個可行的解決方案。
幾番嘗試下來,終究是填了坑。
其實是編譯環境的問題,在什麼環境下編譯生成的依賴包,只能在該環境下使用。Lambda是在Linux機器上執行的,所以必須在Linux上進行編譯,生成的依賴包纔可以使用。
就是這麼簡單。