-- Grant Database Permissions Script for SkyArtShop
-- Run this after database restore or schema changes
-- Usage: sudo -u postgres psql -d skyartshop -f fix-db-permissions.sql

-- Grant table permissions
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO pts;

-- Grant sequence permissions
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO pts;

-- Grant function permissions
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA public TO pts;

-- Set default privileges for future tables
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO pts;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT USAGE, SELECT ON SEQUENCES TO pts;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT EXECUTE ON FUNCTIONS TO pts;

-- Verify permissions
SELECT 
    'Tables accessible: ' || COUNT(*) 
FROM information_schema.table_privileges 
WHERE grantee = 'pts' AND table_schema = 'public';

\echo 'Database permissions granted successfully to user: pts'
