Configure database connections via environment variables in your .env file:
# POS Database ConfigurationPOS_HOST=localhostPOS_PORT=5432POS_NAME=daf_pos_db# Note: POS_USER and POS_PASSWORD are NOT in .env# They come from user login credentials (stored in JWT)
Security: Never commit your .env file to version control. Use different credentials for development, staging, and production environments.
Create individual users for each person who will access the POS system:
-- Create a POS userCREATE USER pos_admin WITH PASSWORD 'secure_password';-- Grant connection privilegeGRANT CONNECT ON DATABASE daf_pos_db TO pos_admin;-- Grant table permissionsGRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO pos_admin;-- Grant sequence permissions (for auto-increment IDs)GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO pos_admin;-- Grant execute on stored proceduresGRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA public TO pos_admin;
4
Create roles (optional)
Use PostgreSQL roles for fine-grained permissions:
-- Create rolesCREATE ROLE pos_readonly;CREATE ROLE pos_manager;CREATE ROLE pos_admin_role;-- Grant appropriate permissions to each roleGRANT SELECT ON ALL TABLES IN SCHEMA public TO pos_readonly;GRANT SELECT, INSERT, UPDATE ON ALL TABLES IN SCHEMA public TO pos_manager;GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO pos_admin_role;-- Assign role to userGRANT pos_manager TO pos_user;
The login endpoint queries user roles:
src/controllers/pos.auth.controller.js:18-24
const result = await pool.query(` SELECT r.rolname FROM pg_roles r JOIN pg_auth_members m ON r.oid = m.roleid JOIN pg_roles u ON u.oid = m.member WHERE u.rolname = (SELECT CURRENT_USER)`);
Create a single database user for the application:
-- Create e-commerce application userCREATE USER ecom_user WITH PASSWORD 'your_secure_password';-- Grant database accessGRANT CONNECT ON DATABASE daf_ecom_db TO ecom_user;-- Grant all privileges on public schemaGRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO ecom_user;GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO ecom_user;-- Set default privileges for future objectsALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO ecom_user;ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT USAGE, SELECT ON SEQUENCES TO ecom_user;
Error: password authentication failed for userCauses:
Incorrect username or password
User doesn’t exist in database
pg_hba.conf doesn’t allow password authentication
Solutions:
-- Check if user existsSELECT * FROM pg_roles WHERE rolname = 'your_user';-- Reset passwordALTER USER your_user WITH PASSWORD 'new_password';
Check pg_hba.conf allows md5 or scram-sha-256 authentication:
# TYPE DATABASE USER ADDRESS METHODhost all all 127.0.0.1/32 md5
Permission denied errors
Error: permission denied for table or must be owner of tableCauses:
Database user lacks required privileges
Solutions:
-- Grant all privileges on a tableGRANT ALL PRIVILEGES ON TABLE cliente TO pos_user;-- Grant all privileges on all tablesGRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO pos_user;-- Grant sequence usage (for auto-increment)GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO pos_user;
Too many connections
Error: sorry, too many clients alreadyCauses:
Not closing POS connection pools
PostgreSQL max_connections limit reached
Solutions:
Ensure all controllers have await pool.end() in finally blocks
Increase PostgreSQL max_connections in postgresql.conf: