Skip to content

Conversation

Sagart-cactus
Copy link
Contributor

@Sagart-cactus Sagart-cactus commented Jul 13, 2025

Description

This PR introduces a new tool, query_postgres, that enables Strands Agents to run read-only SQL queries (SELECT, WITH/CTE) against a PostgreSQL database using natural language.

Key highlights:

  • Blocks all write operations (INSERT, UPDATE, DELETE, etc.) for safety
  • Uses only environment variables (PGHOST, PGPORT, PGDATABASE, PGUSER, PGPASSWORD) for secure credential management
  • Designed for data analytics, dashboards, and business insights use cases
  • Includes comprehensive tests covering query validation, environment variable errors, and query execution
  • Adds optional dependency psycopg2-binary under [project.optional-dependencies.query_postgres]
  • Fully documented with usage examples, environment variable reference, and README integration

Related Issues

Closes #154

Documentation PR

N/A (README updates included in this PR)

Type of Change

  • New Tool

Testing

Tool was tested using:

  • hatch fmt --linter
  • hatch fmt --formatter
  • hatch test --all

All tests passed successfully including:

  • test_missing_env_vars
  • test_disallowed_query
  • test_read_only_select (with mocked connection)

Checklist

  • I have read the CONTRIBUTING document

  • I have added tests that prove my fix is effective or my feature works

  • I have updated the documentation accordingly

  • I have added an appropriate example to the documentation to outline the feature

  • My changes generate no new warnings

  • Any dependent changes have been merged and published

  • By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@Sagart-cactus Sagart-cactus requested a review from a team as a code owner July 13, 2025 06:30
@Sagart-cactus Sagart-cactus changed the title 🚀 Add query_postgres tool for read-only natural language queries to PostgreSQL feat(tools): Add query_postgres tool for read-only natural language queries to PostgreSQL Jul 13, 2025
@Sagart-cactus
Copy link
Contributor Author

Not sure why the PR did not assign reviewer for this PR. @zastrowm can you help in reviewing this one too 🙈🙏

@DimitrijeGlibic
Copy link

Any updates on this? Do you think it will be merged soon? I’m looking to use it in a project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEATURE] query_postgres for Executing SQL Queries on PostgreSQL
2 participants