astronomer/astronomer-cosmos

Postgres connection error

Closed this issue ยท 8 comments

I tried to use profile_mapping to airflow working connection as well as working dbt profiles.yml file (profiles_yml_filepath) in profile_config.
In both cases I receive
ERROR - Failed to execute job 36 for task test_run (URI format postgres:// must contain database, schema, and table names; 36818)

Hey there, @KaterynaD! ๐Ÿš€ I'm Dosu, your friendly bot here to tackle bugs, field questions, and guide you into becoming a super contributor while the human maintainers are away. I'm all ears and ready to dive into your issue about the Postgres connection error. Hang tight, I'll get back to you with some insights shortly! ๐Ÿค–

Hi @KaterynaD, please, could you give us the details to reproduce this problem, including version of Cosmos and your DAG?

To address the Postgres connection error you're encountering, it's crucial to ensure that the URI for the Postgres connection includes the necessary components: database name, schema, and table names. This issue often arises from an incorrect or incomplete configuration in the profiles.yml file or the profile_mapping setup.

For a more targeted solution, please provide:

  • The version of Cosmos you're using, as configuration steps can vary between versions.
  • The configuration of your Apache Airflow DAG, especially how it's set up to use dbt and Cosmos with profile_mapping or profiles.yml.
  • The contents of your profiles.yml file or the profile_mapping configuration, to check if the URI is constructed correctly.
  • Details on how Cosmos is configured in relation to ProfileConfig and ProjectConfig.

This information will help pinpoint the cause of the error and guide you towards a resolution.

Details

Sources


About Dosu
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a ๐Ÿ‘ if this is helpful and ๐Ÿ‘Ž if it is irrelevant.

To continue the conversation, mention @dosu.

@dosu.

Hello Tatiana,

Here are more details:

versions

Python: 3.12.2
airflow: 2.9.0
dbt core: 1.7.14
cosmos: 1.3.2

profiles.yml

psql_test:
outputs:
dev:
dbname: airflow
host: 192.168.1.201
pass: xyz
port: 5432
schema: test_schema
threads: 1
type: postgres
user: kd
target: dev

DAG and profile_config

from datetime import datetime
import os

from airflow.utils.dates import days_ago

from cosmos import DbtDag, ProjectConfig, ProfileConfig, ExecutionConfig
from cosmos.profiles import PostgresUserPasswordProfileMapping

HOME = os.environ["AIRFLOW_HOME"] # retrieve the location of your home folder
dbt_path = os.path.join(HOME, "dags/transformations/psql_test") # path to your dbt project

profile_config = ProfileConfig(
profile_name="psql_test",
target_name="dev",
profiles_yml_filepath=f"{dbt_path}/profiles.yml",
)

basic_cosmos_dag = DbtDag(
dag_id='dbt_cosmos_dag',
project_config =ProjectConfig(dbt_path,),
operator_args={
"install_deps": True, # install any necessary dependencies before running any dbt command
},
profile_config=profile_config,
#execution_config=ExecutionConfig(dbt_executable_path=f"{os.environ['AIRFLOW_HOME']}/dbt_venv/bin/dbt",),
# normal dag parameters
default_args={'start_date': days_ago(1)},
schedule_interval='0 23 * * *',
catchup=False
)

The version with using Airflow connection mapping was exactly as described in Example Usage (https://github.com/astronomer/astronomer-cosmos)

profile_config = ProfileConfig(
profile_name="default",
target_name="dev",
profile_mapping=PostgresUserPasswordProfileMapping(
conn_id="postgres_default",
profile_args={"schema": "test_schema"},
),
)

The DAG correctly recognized dbt models and displays Graph as expected.

Thanks,
Kate

The error you're encountering with the Postgres connection in your Cosmos setup seems to stem from an incorrect URI format, which unusually requires specifying database, schema, and table names. This requirement isn't standard for dbt or Airflow configurations, which typically involve specifying only the database and schema names in their respective connection setups. Here's how you can address the issue:

  1. Verify Airflow and dbt Configurations: Ensure your Airflow Postgres connection (postgres_default) and your profiles.yml are correctly configured. In profiles.yml, the dbname should correspond to your database name, and schema should be the schema within that database. Airflow's schema parameter should map to the database name, not the schema name as in dbt's context.

  2. Adjust Cosmos Configuration: If Cosmos requires a specific URI format that includes the table name, consider customizing the PostgresUserPasswordProfileMapping to include this detail. However, this approach is non-standard as dbt typically interacts with multiple tables and doesn't require specifying a table name in its profile.

  3. Compatibility Check: Ensure your environment matches the compatibility requirements of Cosmos, which as per its pyproject.toml, supports Python versions 3.8 to 3.11 and requires Apache Airflow version 2.3.0 or higher. Your current Python version (3.12.2) falls outside this range, which might be contributing to the issue.

  4. Consult Documentation and Support: Review the documentation for Cosmos, dbt, and Airflow for any specific integration requirements or recent changes. If the problem persists, consider reaching out to their support forums or communities for further assistance.

Remember, the requirement to include a table name in the URI format for a Postgres connection is unusual for dbt and Airflow usage, which typically operate at the database and schema level. Adjusting your setup to align with standard practices and compatibility requirements should help resolve the connection error.

Details

Sources


About Dosu
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a ๐Ÿ‘ if this is helpful and ๐Ÿ‘Ž if it is irrelevant.

To continue the conversation, mention @dosu.

@KaterynaD this workaround worked for me, when I got same error message: #945 (comment)

The workaround mentioned in issue #945 involves modifying the connection URI format to include the database, schema, and potentially the table name, which is not typically required for dbt or Airflow configurations. This adjustment might be specific to how Cosmos interprets or requires the URI to be formatted for successful connections. If following the workaround resolved the issue for another user, it could be a viable solution for you as well. It's important to review the specific changes suggested in the workaround and apply them to your setup, ensuring they align with your project's requirements and the expected URI format for Cosmos. Additionally, keep an eye on any updates or patches from the Cosmos team that might address this issue in a more straightforward manner in the future.

Details

About Dosu
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a ๐Ÿ‘ if this is helpful and ๐Ÿ‘Ž if it is irrelevant.

To continue the conversation, mention @dosu.

thank you