PostgreSQL
PostgreSQL Integration
Section titled “PostgreSQL Integration”Connect Querri to your PostgreSQL database to run queries, analyze data, and create visualizations.
Overview
Section titled “Overview”The PostgreSQL connector supports:
- PostgreSQL 9.6+: All modern PostgreSQL versions
- AWS RDS PostgreSQL: Managed PostgreSQL on AWS
- Azure Database for PostgreSQL: Managed PostgreSQL on Azure
- Google Cloud SQL for PostgreSQL: Managed PostgreSQL on GCP
- Heroku Postgres: Heroku’s managed PostgreSQL
Prerequisites
Section titled “Prerequisites”- PostgreSQL database server
- Database credentials with appropriate permissions
- Network access to the database
IP Whitelisting
Section titled “IP Whitelisting”Important: For cloud-hosted databases or databases behind firewalls, whitelist the following IP address:
18.189.33.77This is Querri’s outbound IP address. Add this to your database firewall rules or security group.
Creating the Connector
Section titled “Creating the Connector”-
Navigate to Connectors
- Go to Settings → Connectors
- Click “Add Connector”
-
Select PostgreSQL
- Choose “PostgreSQL” from the database connectors
-
Configure Connection
Host: your-database-host.comPort: 5432 (default PostgreSQL port)Database: your_database_nameUsername: your_usernamePassword: your_passwordOptional Settings:
- SSL Mode:
require- Always use SSL (recommended)prefer- Use SSL if availabledisable- Don’t use SSL
- Schema: Default is
public - Connection Timeout: Default is 30 seconds
-
Test Connection
- Click “Test Connection” to verify
- If successful, click “Save”
-
Use in Projects
- Create a new step → “Database Query”
- Select your PostgreSQL connector
- Write your SQL query
Network Configuration
Section titled “Network Configuration”AWS RDS Security Groups
Section titled “AWS RDS Security Groups”Add inbound rule:
Type: PostgreSQLProtocol: TCPPort: 5432Source: 18.189.33.77/32Description: Querri AccessAzure PostgreSQL Firewall
Section titled “Azure PostgreSQL Firewall”Add firewall rule:
Rule Name: QuerriStart IP: 18.189.33.77End IP: 18.189.33.77Google Cloud SQL
Section titled “Google Cloud SQL”Add authorized network:
Name: QuerriNetwork: 18.189.33.77/32Heroku Postgres
Section titled “Heroku Postgres”Heroku Postgres allows connections from any IP by default. No configuration needed.
Database User Setup
Section titled “Database User Setup”Read-Only User (Recommended)
Section titled “Read-Only User (Recommended)”Create a dedicated read-only user for Querri:
-- Create userCREATE USER querri_readonly WITH PASSWORD 'secure_password_here';
-- Grant connect permissionGRANT CONNECT ON DATABASE your_database TO querri_readonly;
-- Grant schema usageGRANT USAGE ON SCHEMA public TO querri_readonly;
-- Grant select on all tablesGRANT SELECT ON ALL TABLES IN SCHEMA public TO querri_readonly;
-- Grant select on future tablesALTER DEFAULT PRIVILEGES IN SCHEMA publicGRANT SELECT ON TABLES TO querri_readonly;Read-Write User (If Needed)
Section titled “Read-Write User (If Needed)”For data updates or inserts:
-- Create userCREATE USER querri_readwrite WITH PASSWORD 'secure_password_here';
-- Grant connect permissionGRANT CONNECT ON DATABASE your_database TO querri_readwrite;
-- Grant schema usageGRANT USAGE ON SCHEMA public TO querri_readwrite;
-- Grant table permissionsGRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO querri_readwrite;
-- Grant sequence permissions (for auto-increment columns)GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO querri_readwrite;
-- Grant future permissionsALTER DEFAULT PRIVILEGES IN SCHEMA publicGRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO querri_readwrite;
ALTER DEFAULT PRIVILEGES IN SCHEMA publicGRANT USAGE, SELECT ON SEQUENCES TO querri_readwrite;Example Queries
Section titled “Example Queries”Basic SELECT
Section titled “Basic SELECT”SELECT customer_id, customer_name, email, created_atFROM customersWHERE status = 'active'ORDER BY created_at DESCLIMIT 100;Aggregate Query
Section titled “Aggregate Query”SELECT DATE_TRUNC('month', order_date) as month, COUNT(*) as order_count, SUM(total_amount) as total_revenue, AVG(total_amount) as avg_order_valueFROM ordersWHERE order_date >= CURRENT_DATE - INTERVAL '12 months'GROUP BY DATE_TRUNC('month', order_date)ORDER BY month DESC;JOIN Query
Section titled “JOIN Query”SELECT c.customer_name, c.email, COUNT(o.order_id) as order_count, SUM(o.total_amount) as lifetime_valueFROM customers cLEFT JOIN orders o ON c.customer_id = o.customer_idWHERE c.status = 'active'GROUP BY c.customer_id, c.customer_name, c.emailHAVING COUNT(o.order_id) > 0ORDER BY lifetime_value DESCLIMIT 50;Window Functions
Section titled “Window Functions”SELECT product_name, category, sales_amount, RANK() OVER (PARTITION BY category ORDER BY sales_amount DESC) as rank_in_category, SUM(sales_amount) OVER (PARTITION BY category) as category_totalFROM product_salesWHERE sale_date >= '2024-01-01';Performance Optimization
Section titled “Performance Optimization”Use Indexes
Section titled “Use Indexes”Ensure frequently queried columns have indexes:
CREATE INDEX idx_orders_customer ON orders(customer_id);CREATE INDEX idx_orders_date ON orders(order_date);CREATE INDEX idx_customers_email ON customers(email);Limit Result Sets
Section titled “Limit Result Sets”Always use LIMIT for large tables:
SELECT * FROM large_tableLIMIT 1000;Use Query Parameters
Section titled “Use Query Parameters”For repeated queries with different values, use parameters:
SELECT * FROM ordersWHERE customer_id = {{customer_id}}AND order_date BETWEEN {{start_date}} AND {{end_date}};Set parameters when executing the step.
EXPLAIN for Query Plans
Section titled “EXPLAIN for Query Plans”Analyze query performance:
EXPLAIN ANALYZESELECT * FROM ordersWHERE customer_id = 123;Common Use Cases
Section titled “Common Use Cases”Customer Analysis
Section titled “Customer Analysis”SELECT customer_segment, COUNT(*) as customer_count, AVG(lifetime_value) as avg_ltv, SUM(lifetime_value) as total_ltvFROM customersGROUP BY customer_segmentORDER BY total_ltv DESC;Sales Trends
Section titled “Sales Trends”SELECT DATE_TRUNC('day', order_date) as date, COUNT(*) as orders, SUM(total_amount) as revenueFROM ordersWHERE order_date >= CURRENT_DATE - INTERVAL '30 days'GROUP BY DATE_TRUNC('day', order_date)ORDER BY date;Inventory Status
Section titled “Inventory Status”SELECT product_name, current_stock, reorder_level, CASE WHEN current_stock <= reorder_level THEN 'Reorder Needed' WHEN current_stock <= reorder_level * 1.5 THEN 'Low Stock' ELSE 'Adequate' END as stock_statusFROM inventoryWHERE current_stock <= reorder_level * 2ORDER BY current_stock ASC;Troubleshooting
Section titled “Troubleshooting”Connection Timeout
Section titled “Connection Timeout”Problem: Cannot connect to database
Solutions:
- Verify IP
18.189.33.77is whitelisted - Check database host and port are correct
- Verify database is running and accessible
- Check SSL mode settings
- Verify network connectivity
Authentication Failed
Section titled “Authentication Failed”Problem: Invalid credentials error
Solutions:
- Double-check username and password
- Verify user exists:
SELECT * FROM pg_user WHERE usename = 'your_username'; - Check user has connect permission
- Verify database name is correct
Permission Denied
Section titled “Permission Denied”Problem: Permission denied on table/schema
Solutions:
- Grant necessary permissions (see Database User Setup)
- Verify schema name is correct
- Check table ownership
- Run:
\dp table_nameto see table permissions
SSL Connection Error
Section titled “SSL Connection Error”Problem: SSL connection failed
Solutions:
- Try different SSL modes (prefer, require, disable)
- Verify server SSL certificate is valid
- Check PostgreSQL SSL configuration
- For RDS, ensure SSL is enabled
Query Timeout
Section titled “Query Timeout”Problem: Query takes too long to execute
Solutions:
- Add indexes to improve query performance
- Reduce result set size with LIMIT
- Optimize query with EXPLAIN ANALYZE
- Increase query timeout in connector settings
- Consider creating a materialized view
Security Best Practices
Section titled “Security Best Practices”Use Read-Only Credentials
Section titled “Use Read-Only Credentials”Create dedicated read-only user for analytics:
- Prevents accidental data modification
- Limits potential security impact
- Easier to audit and monitor
Enable SSL
Section titled “Enable SSL”Always use SSL for production databases:
SSL Mode: requireRestrict Permissions
Section titled “Restrict Permissions”Grant only necessary permissions:
-- Only SELECT on specific tablesGRANT SELECT ON TABLE orders, customers TO querri_readonly;
-- Only specific columnsGRANT SELECT (customer_id, email, created_at) ON customers TO querri_readonly;Use Strong Passwords
Section titled “Use Strong Passwords”- Minimum 16 characters
- Mix of letters, numbers, symbols
- Unique for each connector
- Store securely
Regular Audits
Section titled “Regular Audits”Monitor database access:
-- View active connectionsSELECT * FROM pg_stat_activityWHERE usename = 'querri_readonly';
-- View recent queriesSELECT * FROM pg_stat_statementsWHERE userid = (SELECT oid FROM pg_user WHERE usename = 'querri_readonly');Connection Pooling
Section titled “Connection Pooling”Querri uses connection pooling for efficiency:
- Min connections: 1
- Max connections: 10
- Idle timeout: 5 minutes
- Reuse: Connections are reused across queries
PostgreSQL Versions
Section titled “PostgreSQL Versions”Tested and supported versions:
- PostgreSQL 16.x ✓
- PostgreSQL 15.x ✓
- PostgreSQL 14.x ✓
- PostgreSQL 13.x ✓
- PostgreSQL 12.x ✓
- PostgreSQL 11.x ✓ (limited support)
- PostgreSQL 10.x ✓ (limited support)
- PostgreSQL 9.6.x ✓ (limited support)
Support
Section titled “Support”PostgreSQL Resources
Section titled “PostgreSQL Resources”- PostgreSQL Documentation: postgresql.org/docs
- PostgreSQL Wiki: wiki.postgresql.org
- Community Forum: postgresql.org/list
Querri Support
Section titled “Querri Support”- Email: support@querri.com
- Documentation: /guides/connectors/