PostgreSQL Setup
This guide will walk you through configuring your existing PostgreSQL database for use with Sync or Swim. It covers both self-hosted PostgreSQL databases and cloud-hosted services like Amazon RDS, Google Cloud SQL, and Azure Database for PostgreSQL.
Overview
Section titled “Overview”Sync or Swim connects to your PostgreSQL database to synchronize data bidirectionally with other services. You’ll need to:
- Create a dedicated user with appropriate permissions
- Configure network access to allow Sync or Swim connections
- Gather connection credentials
Prerequisites
Section titled “Prerequisites”- Existing PostgreSQL database (version 12 or higher recommended)
- Administrative access to the database server or superuser privileges
- Network access to the database server from where Sync or Swim is running
- Connection details (hostname, port, admin credentials)
Self-Hosted PostgreSQL Database
Section titled “Self-Hosted PostgreSQL Database”This section assumes you already have a PostgreSQL server running and have administrative access to it.
1. Access Your PostgreSQL Database
Section titled “1. Access Your PostgreSQL Database”Connect to your PostgreSQL database using an administrative user:
# Using postgres superuser (Linux/macOS)sudo -u postgres psql
# Or connect with your admin credentialspsql -U your_admin_user -h your_database_host -d postgres
# On Windows, use pgAdmin or psql from Start Menupsql -U postgres2. Create a Database (Optional)
Section titled “2. Create a Database (Optional)”You can either create a new database for Sync or Swim or use an existing one:
-- Option A: Create a new databaseCREATE DATABASE your_database_name;
-- Option B: Use an existing database (skip to step 3)-- Just note the database name you want to use3. Create a Dedicated User
Section titled “3. Create a Dedicated User”-- Create a user for Sync or SwimCREATE USER sync_user WITH PASSWORD 'your_secure_password';
-- Grant privileges on the database (replace 'your_database_name' with your database)GRANT ALL PRIVILEGES ON DATABASE your_database_name TO sync_user;
-- Connect to the database\c your_database_name
-- Grant schema privileges (PostgreSQL 15+)GRANT ALL ON SCHEMA public TO sync_user;GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO sync_user;GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO sync_user;
-- For future tables (PostgreSQL 10+)ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO sync_user;ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO sync_user;4. Configure Network Access (If Required)
Section titled “4. Configure Network Access (If Required)”If Sync or Swim will connect from a different server, you may need to configure PostgreSQL to accept remote connections.
Check if remote access is already configured:
# Test connection from Sync or Swim serverpsql -U sync_user -d your_database_name -h your_database_hostIf the connection fails, you’ll need to configure PostgreSQL:
Find your PostgreSQL config directory:
# Show config file locationsudo -u postgres psql -c 'SHOW config_file;'Edit postgresql.conf:
# Common locations:# RHEL/CentOS: /var/lib/pgsql/[version]/data/postgresql.conf# macOS (Homebrew): /opt/homebrew/var/postgresql@[version]/postgresql.conf
sudo nano /etc/postgresql/16/main/postgresql.confFind and uncomment/modify:
listen_addresses = '*' # Or specify specific IP addresses for better securityport = 5432Edit pg_hba.conf:
sudo nano /etc/postgresql/16/main/pg_hba.confAdd a line to allow connections from Sync or Swim server (adjust as needed for security):
# TYPE DATABASE USER ADDRESS METHOD# Replace with your Sync or Swim server IP addresshost your_database_name sync_user 192.168.1.100/32 scram-sha-256
# Or for a subnethost your_database_name sync_user 192.168.1.0/24 scram-sha-256
# Only use 0.0.0.0/0 for testing, not production!# host your_database_name sync_user 0.0.0.0/0 scram-sha-256Restart PostgreSQL:
# Ubuntu/Debiansudo systemctl restart postgresql
# RHEL/CentOSsudo systemctl restart postgresql-16
# macOS (Homebrew)brew services restart postgresql@165. Verify the Setup
Section titled “5. Verify the Setup”Test the connection from the Sync or Swim server:
# Test connection from Sync or Swim serverpsql -U sync_user -d your_database_name -h your_database_host
# If successful, you should see:# psql (16.x)# Type "help" for help.# your_database_name=>
# Test permissions\dt # Should list tables (if any exist)\q # QuitIf the connection is successful, you’re ready to configure Sync or Swim!
Amazon RDS for PostgreSQL
Section titled “Amazon RDS for PostgreSQL”For existing RDS instances, you’ll need to create a user for Sync or Swim and configure security groups.
1. Configure Security Group
Section titled “1. Configure Security Group”- Navigate to EC2 → Security Groups in AWS Console
- Find the security group assigned to your RDS instance
- Add an inbound rule:
- Type: PostgreSQL
- Protocol: TCP
- Port: 5432
- Source: Your Sync or Swim server’s IP address or security group
2. Create Application User
Section titled “2. Create Application User”Connect to your RDS instance using your master/admin user:
psql -h your-rds-endpoint.region.rds.amazonaws.com \ -U your_master_user \ -d postgresCreate a dedicated user for Sync or Swim:
-- Optional: Create database if you don't have one-- CREATE DATABASE your_database_name;
-- Create application userCREATE USER sync_user WITH PASSWORD 'your_secure_password';
-- Grant privileges (use your database name)GRANT ALL PRIVILEGES ON DATABASE your_database_name TO sync_user;
-- Connect to the database\c your_database_name
GRANT ALL ON SCHEMA public TO sync_user;GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO sync_user;GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO sync_user;ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO sync_user;ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO sync_user;3. Connection Details
Section titled “3. Connection Details”You’ll need:
- Hostname: Found in RDS console (e.g.,
your-instance.abc123.us-east-1.rds.amazonaws.com) - Port:
5432(default) - Database: Your database name
- Username:
sync_user - Password: The password you set
- SSL:
true(RDS requires SSL by default)
Google Cloud SQL for PostgreSQL
Section titled “Google Cloud SQL for PostgreSQL”For existing Cloud SQL instances, you’ll need to configure authorized networks and create a user.
1. Configure Network Access
Section titled “1. Configure Network Access”- Navigate to your Cloud SQL instance in the Cloud SQL Console
- Go to Connections → Networking
- Under Authorized networks, add your Sync or Swim server’s IP address
2. Create Database and User
Section titled “2. Create Database and User”Connect to your Cloud SQL instance:
# Using gcloudgcloud sql connect your-instance-name --user=postgres
# Or using psql with public IPpsql -h your-instance-ip -U postgres -d postgresCreate database and user:
-- Optional: Create database if you don't have one-- CREATE DATABASE your_database_name;
-- Create userCREATE USER sync_user WITH PASSWORD 'your_secure_password';
-- Grant privileges (use your database name)GRANT ALL PRIVILEGES ON DATABASE your_database_name TO sync_user;
-- Connect to the database\c your_database_name
GRANT ALL ON SCHEMA public TO sync_user;GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO sync_user;GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO sync_user;ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO sync_user;ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO sync_user;3. Connection Details
Section titled “3. Connection Details”You’ll need:
- Hostname: Found in Cloud SQL console (public IP address, or use Cloud SQL Proxy)
- Port:
5432 - Database: Your database name
- Username:
sync_user - Password: The password you set
- SSL:
true(recommended)
Azure Database for PostgreSQL
Section titled “Azure Database for PostgreSQL”For existing Azure Database for PostgreSQL instances, you’ll need to configure firewall rules and create a user.
1. Configure Firewall Rules
Section titled “1. Configure Firewall Rules”- Navigate to your Azure Database for PostgreSQL in the Azure Portal
- Go to Connection security or Networking
- Add a firewall rule with your Sync or Swim server’s IP address
2. Create Database and User
Section titled “2. Create Database and User”Connect to your Azure Database using the admin user:
psql "host=your-server.postgres.database.azure.com \ port=5432 \ dbname=postgres \ user=your_admin_user \ sslmode=require"Create database and user:
-- Optional: Create database if you don't have one-- CREATE DATABASE your_database_name;
-- Create userCREATE USER sync_user WITH PASSWORD 'your_secure_password';
-- Grant privileges (use your database name)GRANT ALL PRIVILEGES ON DATABASE your_database_name TO sync_user;
-- Connect to the database\c your_database_name
GRANT ALL ON SCHEMA public TO sync_user;GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO sync_user;GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO sync_user;ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO sync_user;ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO sync_user;3. Connection Details
Section titled “3. Connection Details”You’ll need:
- Hostname:
your-server.postgres.database.azure.com - Port:
5432 - Database: Your database name
- Username:
sync_user@your-server(Azure may require this format) - Password: The password you set
- SSL:
true(required by Azure)
Configuring Sync or Swim
Section titled “Configuring Sync or Swim”Once your PostgreSQL database is set up, configure Sync or Swim with your connection details:
# In your sync_config.yaml or via the UIservices: - name: "my_postgres_database" type: "postgres" credentials: hostname: "your-database-host.com" # or IP address port: 5432 database: "your_database_name" username: "sync_user" password: "your_secure_password" ssl: true # Set to true for cloud providers, adjust for self-hostedConnection String Format (Alternative)
Section titled “Connection String Format (Alternative)”Some users prefer connection string format:
postgresql://sync_user:your_secure_password@hostname:5432/your_database_name?sslmode=requireSecurity Best Practices
Section titled “Security Best Practices”1. Use Strong Passwords
Section titled “1. Use Strong Passwords”- Minimum 16 characters
- Mix of uppercase, lowercase, numbers, and special characters
- Use a password manager
- Rotate passwords regularly
2. Limit Network Access
Section titled “2. Limit Network Access”- Use firewall rules to restrict access to known IP addresses
- For cloud providers, use VPC/Private endpoints when possible
- Avoid
0.0.0.0/0in production environments
3. Enable SSL/TLS
Section titled “3. Enable SSL/TLS”Always use SSL for connections, especially over the internet:
-- Verify SSL is enforcedSELECT name, setting FROM pg_settings WHERE name = 'ssl';For cloud providers, SSL is typically required by default.
4. Use Dedicated Users
Section titled “4. Use Dedicated Users”- Create a specific user for Sync or Swim (don’t use the admin user)
- Grant only necessary privileges
- Use different users for different environments (dev, staging, production)
5. Regular Backups
Section titled “5. Regular Backups”Self-hosted PostgreSQL:
# Automated backup scriptpg_dump -U sync_user -d your_database_name -F c -f backup_$(date +%Y%m%d).dumpCloud Providers:
- Ensure automated backups are enabled (RDS, Cloud SQL, Azure all support this)
- Verify appropriate retention periods (7-30 days)
- Test restore procedures regularly
6. Monitor Connections
Section titled “6. Monitor Connections”-- View active connectionsSELECT pid, usename, application_name, client_addr, stateFROM pg_stat_activityWHERE datname = 'your_database_name';Troubleshooting
Section titled “Troubleshooting”Connection Refused
Section titled “Connection Refused”Symptoms: could not connect to server: Connection refused
Solutions:
- Verify PostgreSQL is running:
sudo systemctl status postgresql - Check firewall rules allow port 5432
- Verify
listen_addressesinpostgresql.conf - Check cloud provider security groups/firewall rules
Authentication Failed
Section titled “Authentication Failed”Symptoms: password authentication failed for user
Solutions:
- Verify username and password are correct
- Check
pg_hba.confallows connections from your IP (self-hosted) - Ensure authentication method is correct (md5, scram-sha-256)
- For cloud providers, verify firewall/security group settings
- For Azure, ensure username format is correct (may need
sync_user@servername)
SSL Connection Required
Section titled “SSL Connection Required”Symptoms: connection requires SSL or sslmode
Solutions:
- Set
ssl: truein Sync or Swim configuration - For cloud providers, SSL is typically required
- Verify SSL certificates are properly configured
Permission Denied
Section titled “Permission Denied”Symptoms: permission denied for schema public or permission denied for table
Solutions:
- Re-run the GRANT statements in the setup section
- Verify user has necessary privileges:
\duin psql - Check ownership of database objects:
\dtin psql - Ensure you connected to the correct database when running GRANT statements
Performance Considerations
Section titled “Performance Considerations”For production environments with high synchronization volumes, consider:
Connection Pooling
Section titled “Connection Pooling”Sync or Swim uses connection pooling by default. For very high loads, you may want to configure connection pool settings based on your database capacity.
Monitor Performance
Section titled “Monitor Performance”-- Check database sizeSELECT pg_size_pretty(pg_database_size('your_database_name'));
-- Monitor active queriesSELECT pid, now() - pg_stat_activity.query_start AS duration, queryFROM pg_stat_activityWHERE state = 'active'ORDER BY duration DESC;
-- Check table sizesSELECT schemaname, tablename, pg_size_pretty(pg_total_relation_size(schemaname||'.'||tablename)) AS sizeFROM pg_tablesWHERE schemaname = 'public'ORDER BY pg_total_relation_size(schemaname||'.'||tablename) DESC;Additional Resources
Section titled “Additional Resources”- PostgreSQL Official Documentation
- Amazon RDS for PostgreSQL
- Google Cloud SQL for PostgreSQL
- Azure Database for PostgreSQL
Support
Section titled “Support”If you encounter issues not covered in this guide, please contact Sync or Swim support with:
- PostgreSQL version:
SELECT version(); - Error messages from the Sync or Swim logs
- Connection details (hostname, port, database name - not passwords!)
- Cloud provider and service tier (if applicable)