Let V ⊂ R n , n ≥ 2, be an unbounded algebraic set defined by a system of polynomial equations h1(x) = • • • = hr(x) = 0 and let f : R n → R be a polynomial. It is known that if f is positive on V then f |V extends to a positive polynomial on the ambient space R n , provided V is a variety. We give a constructive proof of this fact for an arbitrary algebraic set V . Precisely, if f is positive on V then there exists a polynomial h(x) = r i=1 h 2 i (x)σi(x), where σi are sums of squares of polynomials of degree at most p, such that f (x)+h(x) > 0 for x ∈ R n . We give an estimate for p in terms of: the degree of f , the degrees of hi and the Łojasiewicz exponent at infinity of f |V . We prove a version of the above result for polynomials positive on semialgebraic sets. We also obtain a nonnegative extension of some odd power of f which is nonnegative on an irreducible algebraic set.