# Buggy code
def make_site_config(db_name=None, db_password=None, site_config=None, db_type=None):
    frappe.create_folder(os.path.join(frappe.local.site_path))
    site_file = get_site_config_path()

    if not os.path.exists(site_file):
        if not (site_config and isinstance(site_config, dict)):
            site_config = get_conf_params(db_name, db_password)

            if db_type:
                site_config["db_type"] = db_type

        with open(site_file, "w") as f:
            f.write(json.dumps(site_config, indent=1, sort_keys=True))


# Traceback (psycopg2.OperationalError)
$ bench new-site mypgsql.localhost --db-type postgres --db-host postgresql
WARN: bench is installed in editable mode!

This is not the recommended mode of installation for production. Instead, install the package from PyPI with: `pip install frappe-bench`

Traceback (most recent call last):
File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/workspace/development/frappe-bench/apps/frappe/frappe/utils/bench_helper.py", line 99, in <module>
main()
File "/workspace/development/frappe-bench/apps/frappe/frappe/utils/bench_helper.py", line 18, in main
click.Group(commands=commands)(prog_name='bench')
File "/workspace/development/frappe-bench/env/lib/python3.7/site-packages/click/core.py", line 764, in __call__
return self.main(*args, **kwargs)
File "/workspace/development/frappe-bench/env/lib/python3.7/site-packages/click/core.py", line 717, in main
rv = self.invoke(ctx)
File "/workspace/development/frappe-bench/env/lib/python3.7/site-packages/click/core.py", line 1137, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/workspace/development/frappe-bench/env/lib/python3.7/site-packages/click/core.py", line 1137, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/workspace/development/frappe-bench/env/lib/python3.7/site-packages/click/core.py", line 956, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/workspace/development/frappe-bench/env/lib/python3.7/site-packages/click/core.py", line 555, in invoke
return callback(*args, **kwargs)
File "/workspace/development/frappe-bench/apps/frappe/frappe/commands/site.py", line 47, in new_site
db_port=db_port, new_site=True)
File "/workspace/development/frappe-bench/apps/frappe/frappe/commands/site.py", line 87, in _new_site
db_password=db_password, db_type=db_type, db_host=db_host, db_port=db_port, no_mariadb_socket=no_mariadb_socket)
File "/workspace/development/frappe-bench/apps/frappe/frappe/installer.py", line 27, in install_db
setup_database(force, source_sql, verbose, no_mariadb_socket)
File "/workspace/development/frappe-bench/apps/frappe/frappe/database/__init__.py", line 13, in setup_database
return frappe.database.postgres.setup_db.setup_database(force, source_sql, verbose)
File "/workspace/development/frappe-bench/apps/frappe/frappe/database/postgres/setup_db.py", line 6, in setup_database
root_conn.commit()
File "/workspace/development/frappe-bench/apps/frappe/frappe/database/database.py", line 737, in commit
self.sql("commit")
File "/workspace/development/frappe-bench/apps/frappe/frappe/database/postgres/database.py", line 103, in sql
return super(PostgresDatabase, self).sql(*args, **kwargs)
File "/workspace/development/frappe-bench/apps/frappe/frappe/database/database.py", line 122, in sql
self.connect()
File "/workspace/development/frappe-bench/apps/frappe/frappe/database/database.py", line 75, in connect
self._conn = self.get_connection()
File "/workspace/development/frappe-bench/apps/frappe/frappe/database/postgres/database.py", line 69, in get_connection
self.host, self.user, self.user, self.password, self.port
File "/workspace/development/frappe-bench/env/lib/python3.7/site-packages/psycopg2/__init__.py", line 126, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?

# Fixed code
def make_site_config(
    db_name=None,
    db_password=None,
    site_config=None,
    db_type=None,
    db_host=None,
    db_port=None,
):
    frappe.create_folder(os.path.join(frappe.local.site_path))
    site_file = get_site_config_path()

    if not os.path.exists(site_file):
        if not (site_config and isinstance(site_config, dict)):
            site_config = get_conf_params(db_name, db_password)

            if db_type:
                site_config["db_type"] = db_type

            if db_host:
                site_config["db_host"] = db_host

            if db_port:
                site_config["db_port"] = db_port

        with open(site_file, "w") as f:
            f.write(json.dumps(site_config, indent=1, sort_keys=True))