# pgqb 🗄️
A sleek and interactive PostgreSQL query builder for the terminal! 🚀
`pgqb` is a command-line tool that helps you build and execute SQL queries interactively. No more typing complex queries by hand – just select your tables, columns, filters, and more through an intuitive interface!
## Features ✨
- **Interactive Query Building**: Step-by-step selection of schemas, tables, columns, WHERE clauses, ORDER BY, and LIMIT.
- **Fuzzy Search**: Quickly find tables with fuzzy search support.
- **Rich Descriptions**: View table and column descriptions (if available) with aligned formatting.
- **Multiple Output Formats**: Export results as CSV, JSON, JSON Lines, or TSV.
- **Flexible Output Destinations**: Save to files or pipe to stdout (with pager support via shell).
- **Safe Execution**: Confirm queries before execution, with an option to skip confirmations.
- **Cross-Platform**: Works on Linux, macOS, and Windows.
## Installation 🛠️
Install directly from crates.io:
```bash
cargo install pgqb
```
Or build from source:
1. Ensure you have Rust installed: [rustup.rs](https://rustup.rs/)
2. Clone the repository:
```bash
git clone https://github.com/PKWadsy/pgqb.git
cd pgqb
```
3. Build and install:
```bash
cargo build --release
cargo install --path .
```
## Usage 📖
### Basic Usage
```bash
pgqb "postgresql://user:password@localhost/dbname"
```
This starts the interactive query builder. Follow the prompts to select your table, columns, and options.
### Command-Line Options
- `--conn <CONNECTION_STRING>`: PostgreSQL connection string (required)
- `--details`: Show table and column descriptions
- `--exec`: Execute the generated query
- `--no-confirm`: Skip confirmation prompts
- `--format <FORMAT>`: Output format (csv, json, jsonl, tsv)
- `--output <DEST>`: Output destination (file path)
### Examples
1. **Interactive mode with descriptions:**
```bash
pgqb --conn "postgresql://user:pass@localhost/db" --details
```
2. **Execute query and save as CSV:**
```bash
pgqb --conn "postgresql://user:pass@localhost/db" --exec --format csv --output results.csv
```
3. **Pipe to pager:**
```bash
pgqb --conn "postgresql://user:pass@localhost/db" --exec --format table | less
```
## How It Works 🔍
1. **Select Schema & Table**: Choose from available schemas and tables with fuzzy search.
2. **Pick Columns**: Multi-select columns to include (or select all with `*`).
3. **Add Filters**: Enter WHERE clauses with column name autocompletion.
4. **Sort & Limit**: Optionally add ORDER BY and LIMIT clauses.
5. **Execute & Export**: Run the query and choose output format/destination.
The tool generates clean, properly formatted SQL queries and handles various PostgreSQL data types.
## Output Formats 📊
- **CSV**: Comma-separated values with headers
- **JSON**: Pretty-printed JSON array of objects
- **JSONL**: JSON Lines (one JSON object per line)
- **TSV**: Tab-separated values
## Contributing 🤝
Contributions welcome! Please feel free to submit issues, feature requests, or pull requests.
## License 📄
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
---
Made with ❤️ and lots of ☕