You do not say, but I assume that you are running postgres on MacOS or Linux.
The first form (without -h) connects through a "unix socket". The second form with "-h localhost" connects via TCP / IP to localhost (IP address 127.0.0.1).
Postgres - - , , . pgsql/data/pg_hba.conf, , .