why do I need \connect hostdb
That's needed because the target database for the rest of that script is created just in the line above that statement. However creating a new database doesn't automatically switch to that database. In order for all following statements to be executed against that new database, the connection needs to be switched to that database.