TITLE: Prove that for each prime $p$ there exists a nonabelian group of order $p^3$
QUESTION [5 upvotes]: Can anyone helo me out with this question: "Prove that for each prime there exists a nonabelian group of order $p^3$".
This is my attempt:
I know how to show that $|Z(G)|= p$.
So, since $C(x)$ contains $Z(G)$ for every $x$ not in $G$ then $|C(x)|> |Z(G)|$ i.e. $|C(x)|> p$. But $C(x)$ is always a subgroup of $G$ so it’s order must divide the order of $G$ and so $|C(x)= p^2$. (If $|C(x)|= p^3$ then $|G:C(x)|= |G|/|C(x)| = p^3/p^3 = 1$, so the number of elements in every conjugacy class would be 1, which would imply that every element is in the center of $G$, which in turn would mean that $G$ is abelian, which is not what we're after.)
If $|C(x)|= p^2$ then $|G:C(x)|= |G|/|C(x)| = p^3/p^2= p$.
Here’s where I don’t know how to go forward. I know that if there would be $p^2 – 1$ conjugacy classes then  $|G |= |Z(G)|+ (p^2-1) \cdot|G:C(x)|= p + (p^2-1)p = p + p^3 – p = p^3$.
But how do I know that there actually exists a group with exactly $p^2 – 1$ conjugacy classes?
Greatful for input on my reasoning and also other ways of proving this, including how to construct such a group.

REPLY [9 votes]: Your approach doesn't seem fruitful. All of the techniques you are using could at best show that it's not impossible to have an abelian group of order $p^3$. :)
Basically this comes down to the fact that $|\text{Aut}(\mathbb{F}_p^2)|=(p^2-1)(p^2-p)$, and so, in particular is divisible by $p$. Thus, there is a non-trivial semi-direct product $\mathbb{F}_p^2\rtimes (\mathbb{Z}/p\mathbb{Z})$.
Explicitly you can construct a non-abelian group of order $p^3$ by looking at the Heisenberg group:
$$H(p):=\left\{\begin{pmatrix}1 & a & b\\ 0 & 1 & c\\ 0 & 0 & 1\end{pmatrix}:a,b,c\in\mathbb{F}_p\right\}$$
It's probably worth mentioning that there are always exactly $5$ groups of order $p^3$, two non-abelian, for each prime $p$. See here.
I feel like it's worth mentioning the following interest fact:
Call a number $n$ nilpotent if $n=p_1^{e_1}\cdots p_m^{e_m}$ such that for all $i,j$ and for all $1\leqslant k\leqslant e_i$ we have that $p_i^k\not\equiv 1\mod p_j$.

Theorem:

All groups of order $n$ are nilpotent if and only if $n$ is a nilpotent number.

All groups of order $n$ are abelian if and only if $n$ is a cubefree nilpotent number.

All groups of order $n$ are cyclic if and only if $n$ is a squarefree nilpotent number (this is equivalent to $(n,\varphi(n))=1$).