Subject: pgbench without dbname worked differently with psql and pg_dump

From: "Hayato Kuroda (Fujitsu)" <[email-masked]>
Date: Tue, 21 Jan 2025 07:03:05 +0000

Dear hackers,

Recently the 'd' option of pgbench is unified with other applications like psql and pg_dump,
but I found further difference. pgbebch uses an OS user as the dbanme even when the '-U' is
specified. Please see below experiments.

```
# Assuming the OS user is "hayato", whereas the database admin is "postgres"
$ initdb -U postgres -D data
...
Success. You can now start the database server using:

    pg_ctl -D data -l logfile start

$pg_ctl -D data -l logfile start
waiting for server to start.... done
server started

# psql can connect to the database "postgres"
$ psql -U postgres -c "SELECT current_database();"
 current_database 
------------------
 postgres
(1 row)

# pg_dump can connect as well
$ pg_dump -U postgres
--
-- PostgreSQL database dump
--
....

# ... but pgbench cannot
$ pgbench -U postgres
pgbench: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL:  database "hayato" does not exist
pgbench: error: could not create connection for setup
```

Also, I found that dbname parameter for both pgbench and pg_dump is described by
the same sentences [1] [2]. This is also confusing.

Is it an expected behavior?

[1]: https://www.postgresql.org/docs/devel/pgbench.html#PGBENCH-OPTION-DBNAME
```
[-d] dbname
[--dbname=]dbname 
Specifies the name of the database to test in. If this is not specified, the environment variable PGDATABASE is used.
If that is not set, the user name specified for the connection is used.
```

[2]: https://www.postgresql.org/docs/devel/app-pgdump.html#:~:text=of%20the%20output.-,dbname,-Specifies%20the%20name
```
dbname
Specifies the name of the database to be dumped. If this is not specified, the environment variable PGDATABASE is used.
If that is not set, the user name specified for the connection is used.
```

Best regards,
Hayato Kuroda
FUJITSU LIMITED