Skip to content

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.

Sync or Swim connects to your PostgreSQL database to synchronize data bidirectionally with other services. You’ll need to:

  1. Create a dedicated user with appropriate permissions
  2. Configure network access to allow Sync or Swim connections
  3. Gather connection credentials
  • 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)

This section assumes you already have a PostgreSQL server running and have administrative access to it.

Connect to your PostgreSQL database using an administrative user:

Terminal window
# Using postgres superuser (Linux/macOS)
sudo -u postgres psql
# Or connect with your admin credentials
psql -U your_admin_user -h your_database_host -d postgres
# On Windows, use pgAdmin or psql from Start Menu
psql -U postgres

You can either create a new database for Sync or Swim or use an existing one:

-- Option A: Create a new database
CREATE DATABASE your_database_name;
-- Option B: Use an existing database (skip to step 3)
-- Just note the database name you want to use
-- Create a user for Sync or Swim
CREATE 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;

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:

Terminal window
# Test connection from Sync or Swim server
psql -U sync_user -d your_database_name -h your_database_host

If the connection fails, you’ll need to configure PostgreSQL:

Find your PostgreSQL config directory:

Terminal window
# Show config file location
sudo -u postgres psql -c 'SHOW config_file;'

Edit postgresql.conf:

/etc/postgresql/[version]/main/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.conf

Find and uncomment/modify:

listen_addresses = '*' # Or specify specific IP addresses for better security
port = 5432

Edit pg_hba.conf:

Terminal window
sudo nano /etc/postgresql/16/main/pg_hba.conf

Add 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 address
host your_database_name sync_user 192.168.1.100/32 scram-sha-256
# Or for a subnet
host 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-256

Restart PostgreSQL:

Terminal window
# Ubuntu/Debian
sudo systemctl restart postgresql
# RHEL/CentOS
sudo systemctl restart postgresql-16
# macOS (Homebrew)
brew services restart postgresql@16

Test the connection from the Sync or Swim server:

Terminal window
# Test connection from Sync or Swim server
psql -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 # Quit

You’ll need:

  • Hostname: Your database server’s IP or hostname
  • Port: 5432 (default)
  • Database: Your database name
  • Username: sync_user
  • Password: The password you set
  • SSL: Configure based on your setup

Once your PostgreSQL database is set up, configure Sync or Swim through the web UI:

  1. Navigate to /settings in the Sync or Swim web interface
  2. Click “Add Service”
  3. Select “PostgreSQL” as the adapter type
  4. Enter your connection details:
    • Hostname: Your database host
    • Port: 5432 (default)
    • Database: Your database name
    • Username: sync_user
    • Password: Your secure password
    • SSL: Enable for cloud providers
  5. Click “Test Connection” to verify
  6. Click “Create Service” to save
Security Best Practices
  • Minimum 16 characters
  • Mix of uppercase, lowercase, numbers, and special characters
  • Use a password manager
  • Rotate passwords regularly
  • Use firewall rules to restrict access to known IP addresses
  • For cloud providers, use VPC/Private endpoints when possible
  • Avoid 0.0.0.0/0 in production environments

Always use SSL for connections, especially over the internet:

-- Verify SSL is enforced
SELECT name, setting FROM pg_settings WHERE name = 'ssl';

For cloud providers, SSL is typically required by default.

  • 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)

Self-hosted PostgreSQL:

Terminal window
# Automated backup script
pg_dump -U sync_user -d your_database_name -F c -f backup_$(date +%Y%m%d).dump

Cloud Providers:

  • Ensure automated backups are enabled (RDS, Cloud SQL, Azure all support this)
  • Verify appropriate retention periods (7-30 days)
  • Test restore procedures regularly
-- View active connections
SELECT pid, usename, application_name, client_addr, state
FROM pg_stat_activity
WHERE datname = 'your_database_name';
Troubleshooting

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_addresses in postgresql.conf
  • Check cloud provider security groups/firewall rules

Symptoms: password authentication failed for user

Solutions:

  • Verify username and password are correct
  • Check pg_hba.conf allows 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)

Symptoms: connection requires SSL or sslmode

Solutions:

  • Set ssl: true in Sync or Swim configuration
  • For cloud providers, SSL is typically required
  • Verify SSL certificates are properly configured

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: \du in psql
  • Check ownership of database objects: \dt in psql
  • Ensure you connected to the correct database when running GRANT statements
Performance Considerations

For production environments with high synchronization volumes, consider:

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.

-- Check database size
SELECT pg_size_pretty(pg_database_size('your_database_name'));
-- Monitor active queries
SELECT pid, now() - pg_stat_activity.query_start AS duration, query
FROM pg_stat_activity
WHERE state = 'active'
ORDER BY duration DESC;
-- Check table sizes
SELECT schemaname, tablename,
pg_size_pretty(pg_total_relation_size(schemaname||'.'||tablename)) AS size
FROM pg_tables
WHERE schemaname = 'public'
ORDER BY pg_total_relation_size(schemaname||'.'||tablename) DESC;

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)