PG_TMP(1) General Commands Manual PG_TMP(1)

pg_tmpquickly spin up temporary PostgreSQL database

pg_tmp [-k] [-t [-p port]] [-w timeout] [-o extra-options] [-d datadir]

The pg_tmp utility creates a temporary instance of PostgreSQL and prints the connection URL to standard output. pg_tmp spawns a background process that initializes a new database under /tmp/ephemeralpg.*/${PGVER} which is used by subsequent invocations to reduce startup time. The arguments are as follows:

Keep the temporary directory after the server is shut down.
Shut down and remove the database after the specified timeout. If one or more clients are still connected then pg_tmp sleeps and retries again after the same interval. A value of 0 will leave the server running indefinitely. The default is 60 seconds.
Use a TCP port selected by getsocket(1). A port may be selected using the -p flag. Otherwise the path to a Unix socket is returned.
Specifies extra-options to be passed directly to the postgres(1) binary. These options should usually be surrounded by quotes to ensure that they are passed through as a group.
Specify the temporary directory to use. May be used with the optional arguments initdb, start and is required for stop. If this option is used pg_tmp will not initialize a new database for subsequent invocations use.

base directory in which to create and run the ephemeral instances of postgres(1)

Create a temporary database and run a query:

uri=$(pg_tmp)
psql $uri -f my.sql

Start a temporary server with a custom extension:

uri=$(pg_tmp -o "-c shared_preload_libraries=$PWD/auth_hook")
psql $uri -c "SELECT 1"

pg_ctl(1)

October 1, 2020 OpenBSD 6.8