If you have set the database hostname in a configuration file, but you still get this error, that’s usually caused by either an error in the configuration value or a bug in the script (where it doesn’t actually use the database hostname for the connection parameters).
Again, this error is 100% guaranteed to be caused by using localhost. There is no other situation where this error can occur.