Let \(\mathcal {P}_n\) be the set of real-valued algebraic polynomials of degree at most n, and consider the n-dimensional space of functions \(\begin{aligned} \mathcal {H}_{n,\alpha }:= \Big \{f:\,\int _0^x f(t)\,dt = x^{-\alpha /2} e^{-x/2} p(x),\ p\in \mathcal {P}_n, \ p(0)=0\Big \},\ \ \alpha <1. \end{aligned}\) In this paper we study the sharp constant \(c_{n,\alpha }\) in the weighted Hardy inequality \(\begin{aligned} \int _0^\infty \!\left( \frac{1}{x}\int _0^x \!f(t)\,dt\right) ^{\!\!2} x^{\alpha } dx \le c_{n,\alpha } \int _0^\infty \!f^2(x)\,x^{\alpha } dx, \qquad f\in \mathcal {H}_{n,\alpha }. \end{aligned}\) We relate \(c_{n,\alpha }\) to the smallest eigenvalue of a certain Jacobi matrix, and obtain tight two-sided estimates for \(c_{n,\alpha }\) which show the correct asymptotic behavior of \(c_{n,\alpha }\) as n tends to infinity.