# Buggy code
def install_db(
    root_login="root",
    root_password=None,
    db_name=None,
    source_sql=None,
    admin_password=None,
    verbose=True,
    force=0,
    site_config=None,
    reinstall=False,
    db_type=None,
    db_host=None,
    db_port=None,
    db_password=None,
    no_mariadb_socket=False,
):
    from frappe.database import setup_database

    if not db_type:
        db_type = frappe.conf.db_type or "mariadb"

    make_conf(
        db_name, site_config=site_config, db_password=db_password, db_type=db_type
    )
    frappe.flags.in_install_db = True

    frappe.flags.root_login = root_login
    frappe.flags.root_password = root_password
    setup_database(force, source_sql, verbose, no_mariadb_socket)

    frappe.conf.admin_password = frappe.conf.admin_password or admin_password

    remove_missing_apps()

    frappe.db.create_auth_table()
    frappe.db.create_global_search_table()
    frappe.db.create_user_settings_table()

    frappe.flags.in_install_db = False


# 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 install_db(
    root_login="root",
    root_password=None,
    db_name=None,
    source_sql=None,
    admin_password=None,
    verbose=True,
    force=0,
    site_config=None,
    reinstall=False,
    db_type=None,
    db_host=None,
    db_port=None,
    db_password=None,
    no_mariadb_socket=False,
):
    from frappe.database import setup_database

    if not db_type:
        db_type = frappe.conf.db_type or "mariadb"

    make_conf(
        db_name,
        site_config=site_config,
        db_password=db_password,
        db_type=db_type,
        db_host=db_host,
        db_port=db_port,
    )
    frappe.flags.in_install_db = True

    frappe.flags.root_login = root_login
    frappe.flags.root_password = root_password
    setup_database(force, source_sql, verbose, no_mariadb_socket)

    frappe.conf.admin_password = frappe.conf.admin_password or admin_password

    remove_missing_apps()

    frappe.db.create_auth_table()
    frappe.db.create_global_search_table()
    frappe.db.create_user_settings_table()

    frappe.flags.in_install_db = False