PG_TMP(1) General Commands Manual PG_TMP(1)

pg_tmp
quickly spin up temporary PostgreSQL database

pg_tmp [
-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:
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. The default is 60 seconds.
Use a TCP port selected by getsocket(1) bound to 127.0.0.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 combined with the optional arguments initdb, start, and 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)
June 13, 2018 OpenBSD 6.4