Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[+] allow to specify multi-host connection string for configDB with target_session_attrs #647

Merged
merged 5 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion ENV_VARIABLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ NB! Some variables influence multiple components. Command line parameters overri
- **PW2_WEBNOCOMPONENTLOGS** Don't expose Docker component logs. Default: False
- **PW2_WEBNOSTATSSUMMARY** Don't expose summary metrics and "top queries" on monitored DBs. Default: False
- **PW2_VERBOSE** Logging vebosity. By default warning and errors are logged. Use [-v|-vv] to include [info|debug]. Default: -
- **PW2_PGHOST** Config DB host. Default: localhost
- **PW2_PGHOST** Config DB host(s). Default: localhost
- **PW2_PGPORT** Config DB port. Default: 5432
- **PW2_PGDATABASE** Config DB name. Default: pgwatch2
- **PW2_PGUSER** Config DB user. Default: pgwatch2
Expand Down
3 changes: 3 additions & 0 deletions docs/custom_installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ All examples here assume Ubuntu as OS - but it's basically the same for RedHat f

Default port for the Web UI: **8080**. See ``web.py --help`` for all options.

``target_session_attrs`` is set to ``read-write`` by default.
This allows connecting to rw node when multiple (comma separated) hosts on ``-H / --host / PW2_PGHOST`` (i.e. config DB being PG cluster)

#. Configure a SystemD auto-start service (optional)

Sample startup scripts can be found at */etc/pgwatch2/webpy/startup-scripts/pgwatch2-webui.service* or online
Expand Down
16 changes: 10 additions & 6 deletions webpy/datadb.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ def setConnectionStringForMetrics(conn_string):

def setConnectionString(host, port, dbname, username, password, require_ssl=False, connect_timeout=10):
global connection_string
if password == '': # In case of empty password .pgpass lookup will be used
connection_string = 'host={} port={} dbname={} user={} connect_timeout={} {}'.format(
host, port, dbname, username, connect_timeout, '' if not require_ssl else 'sslmode=require')
else:
connection_string = 'host={} port={} dbname={} user={} password={} connect_timeout={} {}'.format(
host, port, dbname, username, password, connect_timeout, '' if not require_ssl else 'sslmode=require')
connection_string = 'host={} port={} dbname={} user={} {} {} connect_timeout={} {}'.format(
host,
port,
dbname,
username,
'' if password == '' else f'password={password}',
'' if psycopg2.__libpq_version__ < 100000 else 'target_session_attrs="read-write"',
connect_timeout,
'' if not require_ssl else 'sslmode=require')



def getConnection(conn_str=None, autocommit=True):
Expand Down