PostgreSQL Python: Connect to PostgreSQL Database Server
Summary: in this tutorial, you will learn how to connect to the PostgreSQL server in Python using the psycopg2
package.
Creating a virtual environment
First, open the Command Prompt on Windows or Terminal on Unix-like systems.
Second, create a new directory to store the project files such as suppliers
:
Third, create a new virtual environment called venv
using the built-in venv
module:
Finally, activate the virtual environment on Windows:
on Unix-like systems:
Installing the psycopg2 module
First, install the psycopg2
package using the following pip
command:
Second, create the requirements.txt
file:
Creating a new database
First, connect to the PostgreSQL server using the psql
client tool:
Second, create a new database called suppliers
:
Third, exit the psql
:
Connecting to the PostgreSQL database from Python
First, create a configuration file called database.ini
in the project directory to store database connection parameters:
In the database.ini
file, you need to replace the YourUsername
and YourPassword
with the real ones.
Second, create a new file called config.py
in the project directory and define a function called load_config()
that reads configuration data from the database.ini
file:
The config.py
module uses the built-in configparser
module to read data from the database.ini
file.
By using the database.ini
, you can change the PostgreSQL connection parameters when moving the code to different environments such as testing or production.
Notice that if you git source control, you need to add the database.ini
to the .gitignore
file to avoid committing sensitive information to a public repository like GitHub:
Third, create a new file called connect.py
that uses the config.py
module to read the database configuration and connect to the PostgreSQL:
To connect to the suppliers
database, you use the connect()
function of the psycopg2
module.
The connect()
function creates a new database session and returns a new instance of the connection
class.
To call the connect()
function, you specify the PostgreSQL database parameters as a connection string and pass it to the function like this:
Alternatively, you can use keyword arguments:
The following is the list of the connection parameters:
database
: the name of the database that you want to connect.user
: the username used to authenticate.password
: password used to authenticate.host
: database server address e.g., localhost or an IP address.port
: the port number that defaults to 5432 if it is not provided.
Since we use the config.py
module, we can pass the configuration to the connect()
function and unpack it using the **
operator:
The with
statement automatically closes the database connection so you don’t have to call the close()
method explicitly.
Executing the connect.py module
To execute the connect.py
file, you use the following command:
Output:
The output indicates that you have successfully connected to the PostgreSQL server.
Download the project source code
Summary
- Use the
psycopg2
package to connect to the PostgreSQL server from Python. - Call the
connect()
function of thepsycopg2
module to connect to the PostgreSQL server.