Graphitic carbon nitride was prepared by bulk and hard template pyrolysis of melamine. The prepared g-C3N4 as well as N-doped carbon used for comparison exhibited a reasonable catalytic activity in the Knoevenagel condensation between benzaldehyde and ethylcyanoacetate yielding ethyl-α-cyanocinnamate (conversion of benzaldehyde up to 100%, yield of the desired product up to 51%). Under the studied reaction conditions, weak basic sites assigned as C–NC species are responsible for selective formation of the desired product. An increase of tertiary nitrogen content leads to an increase of benzaldehyde conversion with a simultaneous decrease of selectivity towards ethyl-α-cyanocinnamate due to a higher basicity. Carbon nitride obtained using silica as a hard template allows achieving the highest yield of the desired product (50.9%).