TITLE: Is there a univariate rational polynomial which represents only squares in $\mathbb{R}$ and $\mathbb{Q}_2$, but not all other $\mathbb{Q}_p$?
QUESTION [12 upvotes]: Let $K$ be a field; I will say a polynomial $f \in K[X]$ represents an element $a \in K$ if there exists a $b \in K$ such that $f(b) = a$.
Denote by $\mathbb{Q}$, $\mathbb{R}$ and $\mathbb{Q}_p$ the fields of rational, real and $p$-adic numbers respectively. Does there exist a polynomial $f \in \mathbb{Q}[X]$ such that

$f$ represents only squares over $\mathbb{R}$ (but not all squares need to be represented),
$f$ represents only squares over $\mathbb{Q}_2$ (but not all squares need to be represented),
for every prime number $p > 2$, $f$ does not represent only squares over $\mathbb{Q}_p$?

If so, what is the minimal degree such a polynomial must have?
What I have found so far:

A polynomial satisfying $1$ and $3$, but not $2$:
$$
1 + X^2
$$
A polynomial satisfying $1$ and $2$ and which I think might also satisfy $3$, but I do not know how to prove it:
$$
(1 + X^2)(17 + X^2)
$$
Here, $17$ may be replaced with any positive integer with residue $1$ modulo $16$.

REPLY [6 votes]: Clearly degree 1 and 3 polynomials do not satisfy property 1.
The only degree 2 polynomials in $\mathbb{Q}[X]$ with property 2 are squares. To verify this, after a possible change of variables assume that $aX^2+b$ is a polynomial generating only squares over $\mathbb{Q}_2$ with $b\ne 0$.  By setting $X=0$ we know $b$ is a square in $\mathbb{Q}_2$, so multiplying by $b^{-1}$ we can assume $b=1$. Next, write $a=2^nc$ with $n=v_2(a)$ its $2$-adic valuation, so that $c\in \mathbb{Z}_2^{\times}$. 

If $n$ is even, say $n=2m$, set $X=c2^{1-m}$ to find that $4c^2+1$ is a square in $\mathbb{Q}_2$, but we reach a contradiction as $5$ is not a square modulo $8$. 
If $n$ is odd, say $n=2m-1$, set $x=2^{-m}$ to yield that $c/2+1$ is a square in $\mathbb{Q}_2$, which is impossible because its valuation is $-1$. 

Finally, there do exist polynomials of degree 4 satisfying all three properties. In particular $f(X)=(8X^2+1)(8X^2+9)$ does the job. Property 1 is clear, so let us check property 2.
The (extended) Hensel's lemma tells you that if a polynomial in $\mathbb{Q}_2[X]$ has a root modulo 8 whose derivative at that root is not divisible by 4, then the polynomial has a root in $\mathbb{Q}_2$. Let $a\in\mathbb{Q}_2$ be arbitrary, and write $a=2^nb$ with $b\in \mathbb{Z}_2^{\times}$. When $n\ge -1$, we can apply the above to $X^2-(8a^2+1)(8a^2+9)$. When $n<-1$, we can see that $$f(a)=2^{2(2n+3)}(b^2+2^{-2n-3})(b^2+9\cdot 2^{-2n-3})$$ and thus apply Hensel's to $X^2-2^{-2(2n+3)}f(a)$. 
As for property 3 (inspired by user mercio's comment below), Let $p$ be an odd prime and suppose $f(X)$ only generates squares over $\mathbb{Q}_p$. As $f(1)\equiv 3\pmod{9}$, we can assume $p\ne 3$. Then the elliptic curve $$E:y^2=(8x^2+1)(8x^2+9)$$ has good reduction at $p$ (Wolphram tells me the discriminant is $2^{34}\cdot 9$). Under the assumption $f(x)$ is a square mod $p$ for any $x$, the number of points on $E$ must be at least $2p-4$. The Hasse bound yields at most $2\sqrt{p}+p$ points (note we ignore the point at infinity). Thus, $$2p-4\le 2\sqrt{p}+p$$ which is false for $p\ge 5$. In particular there is an $x$ with $(8x^2+1)(8x^2+9)$ not a square mod $p$, thus $f(X)$ cannot generate only squares in $\mathbb{Q}(X)$.
(Numerical data suggests in fact that more or less half of the numbers $(8x^2+1)(8x^2+9)$ for $x\in\mathbb{F}_p$ are quadratic residues). 
The polynomial suggested by the OP works as well, the proof for property three works equally well except additionally one has to exhibit a nonsquare for the prime $17$ ($x=2$ works).