Postgresql

Python connect to PostgreSQL database

Open new Postgresql database connection, credentials are loaded from .env file. Incorrect credentials will raise an appropriate error. To edit the credentials add them in define new connection recipe.

pythonpostgresqlsqlpsycopg.envpython-dotenv

Required packages

You need below packages to use the code generated by recipe. All packages are automatically installed in MLJAR Studio.

psycopg>=3.2.1

python-dotenv>=1.0.1

Interactive recipe

You can use below interactive recipe to generate code. This recipe is available in MLJAR Studio.

Python code

# Python code will be here

Code explanation

  1. Load credentials from .env file.
  2. Try to establish connection with database.
  3. If checked, display the connection.