1{ lib, pkgs, ... }:
2{
3 name = "pgbouncer";
4
5 meta = with lib.maintainers; {
6 maintainers = [ _1000101 ];
7 };
8
9 nodes = {
10 one =
11 { pkgs, ... }:
12 {
13 systemd.services.postgresql-setup = {
14 postStart = ''
15 ${pkgs.postgresql}/bin/psql -U postgres -c "ALTER ROLE testuser WITH LOGIN PASSWORD 'testpass'";
16 ${pkgs.postgresql}/bin/psql -U postgres -c "ALTER DATABASE testdb OWNER TO testuser;";
17 '';
18 };
19
20 services = {
21 postgresql = {
22 enable = true;
23 ensureDatabases = [ "testdb" ];
24 ensureUsers = [ { name = "testuser"; } ];
25 authentication = ''
26 local testdb testuser scram-sha-256
27 '';
28 };
29
30 pgbouncer = {
31 enable = true;
32 openFirewall = true;
33 settings = {
34 pgbouncer = {
35 listen_addr = "localhost";
36 auth_type = "scram-sha-256";
37 auth_file = builtins.toFile "pgbouncer-users.txt" ''
38 "testuser" "testpass"
39 '';
40 };
41 databases = {
42 test = "host=/run/postgresql port=5432 auth_user=testuser dbname=testdb";
43 };
44 };
45 };
46 };
47 };
48 };
49
50 testScript = ''
51 start_all()
52 one.wait_for_unit("default.target")
53 one.require_unit_state("pgbouncer.service", "active")
54
55 # Test if we can make a query through PgBouncer
56 one.wait_until_succeeds(
57 "psql 'postgres://testuser:testpass@localhost:6432/test' -c 'SELECT 1;'"
58 )
59 '';
60}