# Postgres Tasks


This module contains a collection of tasks for interacting with Postgres databases via the psycopg2 library.

# PostgresExecute

class

prefect.tasks.postgres.postgres.PostgresExecute

(db_name=None, user=None, host=None, port=5432, query=None, data=None, commit=False, **kwargs)[source]

Task for executing a query against a Postgres database.

Args:

  • db_name (str): name of Postgres database
  • user (str): user name used to authenticate
  • host (str): database host address
  • port (int, optional): port used to connect to Postgres database, defaults to 5432 if not provided
  • query (str, optional): query to execute against database
  • data (tuple, optional): values to use in query, must be specified using placeholder in query string
  • commit (bool, optional): set to True to commit transaction, defaults to false
  • **kwargs (dict, optional): additional keyword arguments to pass to the Task constructor

methods:                                                                                                                                                       

prefect.tasks.postgres.postgres.PostgresExecute.run

(db_name=None, user=None, host=None, port=5432, query=None, data=None, commit=False, password=None)[source]

Task run method. Executes a query against Postgres database.

Args:

  • db_name (str): name of Postgres database
  • user (str): user name used to authenticate
  • host (str): database host address
  • port (int, optional): port used to connect to Postgres database, defaults to 5432 if not provided
  • query (str, optional): query to execute against database
  • data (tuple, optional): values to use in query, must be specified using placeholder in query string
  • commit (bool, optional): set to True to commit transaction, defaults to false
  • password (str): password used to authenticate; should be provided from a Secret task
Returns:
  • None
Raises:
  • ValueError: if query parameter is None or a blank string
  • DatabaseError: if exception occurs when executing the query



# PostgresExecuteMany

class

prefect.tasks.postgres.postgres.PostgresExecuteMany

(db_name=None, user=None, host=None, port=5432, query=None, data=None, commit=False, **kwargs)[source]

Task for executing many queries against a Postgres database.

Args:

  • db_name (str): name of Postgres database
  • user (str): user name used to authenticate
  • host (str): database host address
  • port (int, optional): port used to connect to Postgres database, defaults to 5432 if not provided
  • query (str, optional): query to execute against database is query string
  • data (List[tuple], optional): list of values to use in query, must be specified using placeholder
  • commit (bool, optional): set to True to commit transaction, defaults to false
  • **kwargs (dict, optional): additional keyword arguments to pass to the Task constructor

methods:                                                                                                                                                       

prefect.tasks.postgres.postgres.PostgresExecuteMany.run

(db_name=None, user=None, host=None, port=5432, query=None, data=None, commit=False, password=None)[source]

Task run method. Executes many queries against Postgres database.

Args:

  • db_name (str): name of Postgres database
  • user (str): user name used to authenticate
  • host (str): database host address
  • port (int, optional): port used to connect to Postgres database, defaults to 5432 if not provided
  • query (str, optional): query to execute against database
  • data (List[tuple], optional): list of values to use in query, must be specified using placeholder
  • commit (bool, optional): set to True to commit transaction, defaults to false
  • password (str): password used to authenticate; should be provided from a Secret task
Returns:
  • None
Raises:
  • ValueError: if query parameter is None or a blank string
  • DatabaseError: if exception occurs when executing the query



# PostgresFetch

class

prefect.tasks.postgres.postgres.PostgresFetch

(db_name=None, user=None, host=None, port=5432, fetch="one", fetch_count=10, query=None, data=None, commit=False, **kwargs)[source]

Task for fetching results of query from Postgres database.

Args:

  • db_name (str): name of Postgres database
  • user (str): user name used to authenticate
  • host (str): database host address
  • port (int, optional): port used to connect to Postgres database, defaults to 5432 if not provided
  • fetch (str, optional): one of "one" "many" or "all", used to determine how many results to fetch from executed query
  • fetch_count (int, optional): if fetch = 'many', determines the number of results to fetch, defaults to 10
  • query (str, optional): query to execute against database
  • data (tuple, optional): values to use in query, must be specified using placeholder is query string
  • commit (bool, optional): set to True to commit transaction, defaults to false
  • **kwargs (dict, optional): additional keyword arguments to pass to the Task constructor

methods:                                                                                                                                                       

prefect.tasks.postgres.postgres.PostgresFetch.run

(db_name=None, user=None, host=None, port=5432, fetch="one", fetch_count=10, query=None, data=None, commit=False, password=None, col_names=False)[source]

Task run method. Executes a query against Postgres database and fetches results.

Args:

  • db_name (str): name of Postgres database
  • user (str): user name used to authenticate
  • host (str): database host address
  • port (int, optional): port used to connect to Postgres database, defaults to 5432 if not provided
  • fetch (str, optional): one of "one" "many" or "all", used to determine how many results to fetch from executed query
  • fetch_count (int, optional): if fetch = 'many', determines the number of results to fetch, defaults to 10
  • query (str, optional): query to execute against database
  • data (tuple, optional): values to use in query, must be specified using placeholder in query string
  • commit (bool, optional): set to True to commit transaction, defaults to false
  • password (str): password used to authenticate; should be provided from a Secret task
  • col_names (bool, optional): set to True to add column names to records, defaults to False
Returns:
  • records (tuple or list of tuples): records from provided query
Raises:
  • ValueError: if query parameter is None or a blank string
  • DatabaseError: if exception occurs when executing the query



This documentation was auto-generated from commit bd9182e
on July 31, 2024 at 18:02 UTC