Re: pgbench without dbname worked differently with psql and pg_dump

From: Ashutosh Bapat <ashutosh(dot)bapat(dot)oss(at)gmail(dot)com>
To: "Hayato Kuroda (Fujitsu)" <kuroda(dot)hayato(at)fujitsu(dot)com>
Cc: "pgsql-hackers(at)lists(dot)postgresql(dot)org" <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: pgbench without dbname worked differently with psql and pg_dump
Date: 2025-01-21 10:44:53
Message-ID: CAExHW5sBWsVswxtunLMqU4G6Wmi859SZVyTV48Pa1CnDpjocrw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Tue, Jan 21, 2025 at 12:33 PM Hayato Kuroda (Fujitsu)
<kuroda(dot)hayato(at)fujitsu(dot)com> wrote:
>
> 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.
> ```

I see the same behaviour. The code in pgbench which decided the
database to connect to, looks similar to the relevant code in
createdb. But createdb documentation is in agreement with the code.
[1]
```
dbname

Specifies the name of the database to be created. The name must be
unique among all PostgreSQL databases in this cluster. The default is
to create a database with the same name as the current system user.
```

I couldn't locate the code in psql which calculates the name of the
database from a quick read of psql/startup.c. I think pgbench should
behave like psql, not like createdb.

[1] https://www.postgresql.org/docs/17/app-createdb.html

--
Best Wishes,
Ashutosh Bapat

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Umar Hayat 2025-01-21 10:48:01 Re: Add XMLNamespaces to XMLElement
Previous Message Hunaid Sohail 2025-01-21 10:17:21 Re: Psql meta-command conninfo+