I’m trying to create a new database using C code and libpq-fe.h
The problem is taht there’s literally not a single example on the internet to show you how to do so, including the docs.
If I try to specify the database name that I want to create in the connection I get an error, if I don’t, I think it connects automatically to postgres database.
But I want to create and connect to a new database, how can I do that?
2
Answers
any particular reason you want to create the database from the app?
I would use dBeaver, best tool for database manipulations.
https://dbeaver.io/download/
anyways have you looked at this:
https://www.tutorialspoint.com/postgresql/postgresql_c_cpp.htm
I’d connect using that and then create a database regularly with an SQL command:
https://www.postgresql.org/docs/current/sql-createdatabase.html
The only way to create a database is to first connect to another database, then run
CREATE DATABASE
there. That’s what thepostgres
database is there for. So do this:connect to database
postgres
execute the
CREATE DATABASE
statementdisconnect from
postgres
connect to the newly created database