Erections are actually quite complex and involve decent function from your cardiovascular, neurological and psychological systems. The psychogenic - the term the medical community prefers for "psychological" - aspect of an erection almost always plays some role in your hardness factor for several critical reasons:
1. Nerves and Neurotransmitters. It's no secret that erections are initiated by a nervous system response. Sexual stimulation fires up various neurotransmitters, such as epinephrine, acetylcholine, oxytocin and nitric oxide for starters. These, in turn, fire up the ol' neurons and you know the rest of the story.
Thus, any nerve-related issues, such as neuropathy, i.e. "nerve damage", can be a partial contributor toerectile dysfunction . Diabetics, who often have accelerated neuropathy, can struggle with this in particular. Researchers can even induce an erection in dogs simply by stimulating the perineal nerves directly. 
One of the beautiful things about PDE5 Inhibitors, such as Viagra and Cialis, is they actually do not work unless the patient is stimulated visually or by touch in a sexual manner. This is because the nervous system must first be stimulated which in turn fires by the endothelial nitric oxide response that these drugs are famous for. And that is a good thing, because otherwise you could get guys getting a spotaneous erection at almost any time of the day!
2. Failure. Generally, erectile dysfunction for middle-aged and beyond guys is endothelial (artery-related) in nature. In addition, low testosterone often exacerbates the condition. These underlying physical conditions often lead to a few performance failures, which can create tremendous anxiety for most guys (and often for their spouses and/or partners as well). This can bring the psychological (psychogenic) part of an erection to the forefront very quickly.
3. Testosterone. The mind plays a huge factor in testosterone levels and decreased testosterone levels can sabotage erectile strength. Depression is a well-known testosterone lowerer and competition a Testosterone Booster.
Tragically, all of this can be made worse by visiting the doctor. The doctor will often do nothing or imply it's just a natural part of aging. The patient is left to struggle alone.
So how do you know if you have psychogenic (pscychological) erectile dysfunction? Well, the gyst of the diagnosis has to do with whether or not a man can achieve a normal erection under some circumstances but just not in the bedroom. Here are a few of the things that they will look at:
1. Morning Erections. Odds are if you can achieve a firm morning erection that you're physically functioning fine. In fact, this has been used by diagnosticians for literally decades as an excellent tool.  In fact, one study noted that "the single best predictor was the presence or absence of early morning erections as reported by the patient." 
2. Rigiscan. Doctors will sometime use the Rigiscan device, which essentially measures your "hardness factor", i.e. the firmness of your erection. Again, if you can get hard when you're not under pressure, then you've likely got psychogenic erectile issues.
Solving psychogenic impotence can be difficult indeed. Many guys, after repeated failures in the bedroom, dread another attempt and risking failure. In addition, sex is about as personal of a subject as one can come up with and, for most males, is tied to their maleness and manhood.
Fortunately, there are reasonable solutions with solid success rates, which are discussed below. One methodology that I won't discuss is sex therapy with a sex therapist, which is the mainstay of such sexological leaders as Masters and Johnson. This methodology has a reasonable success rate but most guys are just not going to do it. (This methodology requires a sex therapist to be present during the meetings with or without your partner. For many, this is just too uncomfortable and/or may not reconcile with one's religious beliefs.
Other solutions include the following:
1. PDE5 Inhibitors. Viagra alone produced a 66% success rate in getting men over their psychogenic erectile dysfunction.  Of course, this is likely because erections come so easily and quickly with Viagra that there is less time for anxiety. However, sexual satisfaction was significantly increased with Cognitive-Behavorial Therapy, which include working with their partner to "identify positive aspects of themselves and their parter; review positive relationship experiences; recall past, present and future ways to share and show love and affection and then prepare a surprise from the list", etc.
2. Intracavernous Injections. In some men the performance anxiety and fear of failure is very high and PDE5 Inhibitors and Therapy just do not work. In these cases physicians can actually give men a set of preloaded syringes for pre-bedroom use. The men has to actually inject his own penis - it doesn't hurt as much as you might think - that literally forces an erection. Men involved in this sort of treatment have "no worries" as the medicine keeps him erect through almost any emotional state. Although it sounds very painful, only one fifth of the men in one study complained of pain and this was correctable in all cases. 
3. Dostinex (Cabergoline). This medication is used to treat men with high prolactin and/or prolactinoma and can, in those cases, boost testosterone and improve the refractory period. Dostinex belongs to the class of medications called dopamine agonists and has a study behind it showing that it may also improve psychogenic E.D. 
The success rate for this strategy is very high. For example, in one study of couples with "honeymoon impotence", 93% of the couples achieved success and only a few needed long term use. The 7% that were unsuccessful, by the way, was due to vaginismus, or vaginal pain.
1) International Journal of Impotence Research, 2002, 14:433-439, "The neuropathy of erectile dysfunction"
2) International Journal of Impotence Research, March 1997, 9(1):11-16, "Perineal nerve stimulation: role in penile erection"
3) J Sex Med, 2007, 4:1117-1125, "Integrated Sildenafil and Cognitive-Behavior Sex Therapy for Psychogenic Erectile Dysfunction: A Pilot Study"
4) Intl J of Impotence Res, 1998, 10:211-314, "Short term use of intracavernous vasoactive drugs in the treatment of persistent psychogenic erectile dysfunction"
5) Journal of Sex & Marital Therapy, 1975, 1(4), "The assessment of nocturnal REM erection in the differential diagnosis of sexual impotence"
6) ARCHIVES OF SEXUAL BEHAVIOR, 1987, 16(2):125-137, "Use of sexual history to differentiate organic from psychogenic impotence"
7) Int J Impot Res, 2007 Jan-Feb ,19(1):104-7, "Cabergoline treatment in men with psychogenic erectile dysfunction: a randomized, double-blind, placebo-controlled study"