如何使用libpqxx以编程方式清除PostgreSQL数据库



对于HelpCovid GPLv3+项目(C++17、Linux/x86-64、Debian/Buster、PostgreSQL 11或12(及其问题#27,我们希望清除给定PostgreSQL 11中的所有数据。我们今天(2020年4月6日,git-commit2843184d9f589d51bd9(只有表和索引(有关详细信息,请参阅DATABASE.md中的文档和C++文件hcv_database.cc(。

我们只想从给定的数据库(由generate-config.pypython脚本初始化(中删除每个表和索引(即"每个数据"(。

我们尝试了几种方法:

  • 使用DROP DATABASEpqxx::nontransaction

  • 使用此处建议的动态SQL。

但到目前为止,每一次尝试都失败了。

在提交cb982e1a57c9de81d中,观察到以下调试输出(C++中用HCV_DEBUGOUT宏输出的调试消息包含ΔBG!(。在./generate-config.py:之后运行./helpcovid --clear-database -D -T 2

./helpcovid[1393556]: HelpCovid cb982e1a57c9 start adding <?hcv confmsg, but --clear-database still does not work (issue#27) program arguments:
... ./helpcovid --clear-database -D -T 2
./helpcovid[1393556]: hcv_main.cc:573 -  !! parsed 5 program arguments
./helpcovid[1393556]: hcv_main.cc:884 -  !! start of ./helpcovid
version:github.com/bstarynk/helpcovid built Mon 06 Apr 2020 08:35:00 AM MEST
... gitcommit cb982e1a57c9 start adding <?hcv confmsg, but --clear-database still does not work (issue#27)
... md5sum 7f39a5002c3afc4a6b242015a9f856bb on rimski
at Mon Apr  6 08:35:15 2020 MEST on rimski
./helpcovid[1393556]: hcv_main.cc:626 -  !! loading configuration file /home/basile/.helpcovidrc
./helpcovid[1393556]: hcv_main.cc:632 -  !! helpcovid loaded configuration file /home/basile/.helpcovidrc
./helpcovid[1393556]: hcv_web.cc:76 -  !! hcv_initialize_web: weburl='http://localhost:8089/', webroot='/home/basile/helpcovid/webroot/', opensslcert='', opensslkey=''
./helpcovid[1393556]: hcv_web.cc:114 -  !! starting plain HTTP server using weburl http://localhost:8089/ and webroot /home/basile/helpcovid/webroot/ hcv_webserver@0x5622aefcb0d0
./helpcovid[1393556]: hcv_main.cc:964 -  !! helpcovid debugging enabled
./helpcovid[1393556]: ΔBG!hcv_main.cc:965▪ 00.00 s‣  helpcovid is debugging
./helpcovid[1393556]: hcv_main.cc:1026 -  !! helpcovid unable to write builtin pidfile /var/run/helpcovid.pid
-: Permission denied
./helpcovid[1393556]: hcv_database.cc:114 -  !! using 'dbname=helpcovid_db user=helpcovid_usr password=passwd1234 hostaddr=127.0.0.1 port=5432' as PostGreSQL connection string.
./helpcovid[1393556]: hcv_database.cc:129 -  !! hcv_initialize_database connstr=dbname=helpcovid_db user=helpcovid_usr password=passwd1234 hostaddr=127.0.0.1 port=5432
./helpcovid[1393556]: hcv_database.cc:133 -  !! hcv_initialize_database for connstr=dbname=helpcovid_db user=helpcovid_usr password=passwd1234 hostaddr=127.0.0.1 port=5432 hcv_dbconn is 0x5622aefcb810
terminate called after throwing an instance of 'pqxx::insufficient_privilege'
what():  ERROR:  must be owner of database helpcovid_db
zsh: abort (core dumped)  ./helpcovid --clear-database -D -T 2

该HelpCovid程序应该使用setuid技术进行部署。出于网络安全原因,我对运行任何外部命令的想法感到不满(使用system(3(、popen(3(或fork(2(+execve(2(+waitpid(2(..(在此阶段清除数据库。

当然,我是一个PostgreSQL新手。

您有两个选择:

drop schema

如果所有内容都存储在一个模式中,并且该模式由用户所有,则使用drop schema ... cascade。有关详细信息,请参阅手册。

注意,public模式通常由超级用户postgres拥有。您需要先转让所有权,然后才能执行此操作。

drop owned

如果要删除的所有(!(都为当前用户所有,则可以使用drop owned ..

这将真正删除您指定的用户所拥有的所有(包括视图、函数、触发器、模式、类型,实际上:一切(。

通常,您将作为所有者进行连接,然后运行drop owned by current_user;

有关的详细信息,请参阅手册

数据库所有者是运行CREATE database的用户:https://www.postgresql.org/docs/12/manage-ag-createdb.html

架构所有者是已运行CREATE schema的用户:https://www.postgresql.org/docs/12/ddl-schemas.html#DDL-SCHEMAS-创建

一个数据库有不同的用户帐户(就像Linux上有不同用户一样(:https://www.postgresql.org/docs/12/sql-createrole.html

从应用程序连接到数据库的用户通常与数据库所有者和模式所有者不同(就像Linux上的root用户是admin.user一样,但除非您是系统管理员,否则大多数时候都不会使用它(。

最新更新