Qualified psychiatrists served aboard starships during the 23rd century. (TOS: "Where No Man Has Gone Before", "Dagger of the Mind") Occasionally, the role of chief medical officer and counselor were combined in the 23rd century, with medical officers also having expertise in space psychology; an example of this is Doctor Leonard McCoy. (TOS: "Court Martial")
By the mid-24th century, starship and starbase crews included a counselor, who was responsible for the mental well-being of the crew and civilian staff. The position was considered vital enough that it warranted inclusion in the senior staff of the Federation flagship; in that instance at least, the ship's counselor also had a diplomatic role, advising the captain in First Contact and other situations.
As with the chief medical officer, the ship's counselor has the power to relieve other officers and crewmembers of duty if he or she feels that their patient is suffering from a condition that may hinder their ability to perform their duties effectively.
At the captain's discretion, the ship's counselor may be allowed to wear something other than their normal Starfleet uniform. This was the case with Deanna Troi, the ship's counselor on the USS Enterprise-D, who preferred to wear colorful, casual clothing while on duty. Troi elected to wear a standard uniform permanently after being told to do so by Captain Edward Jellico. (TNG: "Chain of Command, Part I")
Some Starfleet officers, such as Captain Lisa Cusak and Chief Miles O'Brien, disliked the concept of a designated ship's counselor, preferring to seek advice about their problems from friends. (DS9: "The Sound of Her Voice")
Deanna Troi trained in psychology before joining Starfleet. She served as counselor aboard the USS Enterprise-D and the USS Enterprise-E before transferring to the USS Titan with her husband, William T. Riker. (TNG: "Encounter at Farpoint"; Star Trek Nemesis)
Deep Space 9 had several counselors during its time in operation, including Telnorri and Ezri Dax, who was previously an assistant counselor on the USS Destiny. (DS9: "Hard Time", "Shadows and Symbols")
The USS Voyager was not originally assigned a counselor, as its mission was intended only to last a few weeks. (VOY: "Phage", "The Cloud") Later, Neelix appointed himself morale officer, a position of his own invention, to take on the responsibilities of a counselor. (VOY: "Phage")