I rarely want a port open to the whole internet. A rule with a source network gives access to only the machines I name.
sudo ufw allow from 192.168.1.0/24 to any port 5432 proto tcp
The long form is allow from SOURCE to DEST port PORT proto PROTOCOL. Here only the 192.168.1.0/24 network can reach PostgreSQL, as long as the default policy for incoming traffic is deny. The proto part is required when you name a port range and useful to limit the rule to TCP or UDP. Check the result with sudo ufw status, where the source appears in the From column.
Further reading: ufw manual page