How to fix "This database is not enabled for publication" issue when the replication option is already enabled in SQL Server?

Error 1:

Msg 14013, Level 16, State 1, Procedure sp_MSrepl_addpublication, Line 188 This database is not enabled for publication.

Error 2:

Msg 14013, Level 16, State 1, Procedure sys.sp_MSrepl_addsubscription, Line 302 [Batch Start Line 0] This database is not enabled for publication.

I was getting these errors on different occasions when trying to execute a script in a database to add a publication and add a subscription.

If the script is executed in a database that is not enabled for replication, then this error occurs.

Solution:

To solve this issue, first, check whether the database is published.

-- check database is published or not
select * from sys.databases where is_published=1

If not published, publish the database in the publisher server.

--publish database
use publisher_database_name;
GO

exec sp_replicationdboption 
@dbname = N'publisher_database_name', 
@optname = N'publish', 
@value = N'true'
GO

Then, make sure you are connected to the published database. Execute your script. In my case, I have executed the add publication and add subscriptions. It has worked fine on both occasions. Hope this helps to solve your issue.