Goodnight is never used as a greeting, only a parting salutation, regardless of the time of day.
Technically, you could use 'good night' well into the very early hours of the day up until about dawn, but still only as a parting comment.
For example, if I'd been up all night with a friend until four in the morning, I might still tell them 'good night' as we parted. However, if I immediately encountered someone else, I'd have to use 'good morning' (or some other greeting). I couldn't use 'good night' with the new person.