I am new to the PostgreSQL database. What my visual c++ application needs to do is to create multiple tables and add/retrieve data from them.
Each session of my application should create a new and distinct database. I can use the current date and time for a unique database name.
There should also be an option to delete all the databases.
I have worked out how to connect to a database, create tables, and add data to tables. I am not sure how to make a new database for each run or how to retrieve number and name of databases if user want to clear all databases.
Please help.
See the libpq examples in the documentation. The example program shows you how to list databases, and in general how to execute commands against the database. The example code there is trivial to adapt to creating and dropping databases.
Creating a database is a simple
CREATE DATABASESQL statement, same as any other libpq operation. You must connect to a temporary database (usuallytemplate1) to issue theCREATE DATABASE, then disconnect and make a new connection to the database you just created.Rather than creating new databases, consider creating new schema instead. Much less hassle, since all you need to do is change the
search_pathor prefix your table references, you don’t have to disconnect and reconnect to change schemas. See the documentation on schemas.I question the wisdom of your design, though. It is rarely a good idea for applications to be creating and dropping databases (or tables, except temporary tables) as a normal part of their operation. Maybe if you elaborated on why you want to do this, we can come up with solutions that may be easier and/or perform better than your current approach.