我的软件进程取决于本地PostgreSQL集群中是否存在特定数据库。 PostgreSQL和我的软件进程都是在系统启动时由upstart启动的。在PostgreSQL启动之后和我的软件进程开始之前执行脚本以确保数据库处于正确的状态。
脚本使用pg_dump转储数据库的架构。如果服务器未运行或已准备好进行连接,请参阅以下内容
$ pg_dump -U postgres -s testdb
pg_dump: [archiver (db)] connection to database "testdb" failed: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
如果数据库不存在,我会看到以下内容
$ pg_dump -U postgres -s testdb
pg_dump: [archiver (db)] connection to database "testdb" failed: FATAL: database "testdb" does not exist
似乎有一个时间窗口,即使集群确实包含数据库'testdb',对pg_dump的调用也会报告数据库不存在。我可以通过在服务器启动和运行我的脚本之间添加短暂睡眠来避免此问题。我更喜欢一种不包含任何时间睡眠的解决方案。有什么建议吗?
我在32位Ubuntu 12.04 LTS上运行PostgreSQL 9.1。
答案 0 :(得分:1)
假设这是一个shell脚本,我猜你不是在等待create database命令完成。您可以在后台运行createdb或在后台运行psql。如果你在前台运行它们,这不应该发生,因为它们会阻塞直到数据库完成。