We prove a comparison theorem between the \'etale cohomology of algebraic varieties over Stein compacta and the singular cohomology of their analytifications. We deduce that the field of meromorphic functions in a neighborhood of a connected Stein compact subset of a normal complex space of dimension $n$ has cohomological dimension $n$. As an application of $\textrm{Gal}(\mathbb{C}/\mathbb{R})$-equivariant variants of these results, we obtain a quantitative version of Hilbert's 17th problem on compact subsets of real-analytic spaces.