Connecting to PostgreSQL with psycopg2
Now that we have a nice database to work with, how do we get our application to use it? To make SQL queries from our application, we'll need to install a Python library that can talk directly to our database. In Python, each different SQL product has one or more libraries available that can be used to integrate with it.
For PostgreSQL, the most popular choice is psycopg2
. The psycopg2
library is not a part of the Python standard library, so you'll need to install it on any machine running your application. You can find the most current installation instructions at http://initd.org/psycopg/docs/install.html; however, the preferred method is to use pip
.
For Windows, macOS, and Linux, the following command should work:
$ pip install --user psycopg2-binary
If that doesn't work, or if you'd rather install it from the source, check the requirements on the website. Take note that the psycopg2
library is written...