How do I migrate Postgres database using an intermediate file
When you are restoring a dump taken with -C, the database you specify on the command line is name of the database you connect to in order to execute the creation of the new database.  Therefore it can't be the name of the new database.  It seems like you already know this, you just haven't thought it through.  psql is trying to connect to {dbname} because that is what you explicitly told it to do.
PostgreSQL doesn't &quot;treat&quot; sudo commands differently.  Sudo is in charge, not PostgreSQL.  Different commands do different things, and PostgreSQL is just along for the ride.
You are trying to tell the &quot;postgres&quot; system user to read a file located in  your daily user's home directory.  It probably doesn't have permission to do that.  Again, this is not PostgreSQL's choice.  You could put the file somewhere else (like /tmp) and give it world read permissions, or you could change your pg_hba.conf so you can connect to the database as your daily user rather than needing to sudo all the time.