pgbench without dbname worked differently with psql and pg_dump

From: "Hayato Kuroda (Fujitsu)" <kuroda(dot)hayato(at)fujitsu(dot)com>
To: "'pgsql-hackers(at)lists(dot)postgresql(dot)org'" <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: pgbench without dbname worked differently with psql and pg_dump
Date: 2025-01-21 07:03:05
Message-ID: OSCPR01MB14966BC18CB84D81D8016317FF5E62@OSCPR01MB14966.jpnprd01.prod.outlook.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Bertrand Drouvot 2025-01-21 07:19:55 Re: per backend WAL statistics
Previous Message Corey Huinker 2025-01-21 06:43:17 Re: Statistics Import and Export