=== modified file '.bzrignore'
--- .bzrignore	2012-01-09 16:27:14 +0000
+++ .bzrignore	2012-02-05 05:46:18 +0000
@@ -86,6 +86,7 @@
 tests/cpp_test
 tests/cycle
 tests/drizzle_test
+tests/postgres_test
 tests/gearadmin
 tests/gearman.pi*
 tests/gearman.sql

=== modified file 'libgearman-server/plugins.cc'
--- libgearman-server/plugins.cc	2011-11-29 05:21:54 +0000
+++ libgearman-server/plugins.cc	2012-02-05 05:46:18 +0000
@@ -1,5 +1,5 @@
 /*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
- * 
+ *
  *  Gearmand client and server library.
  *
  *  Copyright (C) 2011 Data Differential, http://datadifferential.com/
@@ -63,9 +63,11 @@
     queue::initialize_sqlite();
   }
 
-#ifdef HAVE_LIBPQ
-  queue::initialize_postgres();
-#endif
+  if (HAVE_LIBPQ)
+  {
+    queue::initialize_postgres();
+  }
+
 
 #ifdef HAVE_LIBHIREDIS
   queue::initialize_redis();

=== modified file 'libgearman-server/plugins/queue/postgres/include.am'
--- libgearman-server/plugins/queue/postgres/include.am	2011-08-16 21:55:47 +0000
+++ libgearman-server/plugins/queue/postgres/include.am	2012-02-05 05:46:18 +0000
@@ -12,7 +12,9 @@
 noinst_HEADERS+= libgearman-server/plugins/queue/postgres/queue.h
 
 if HAVE_LIBPQ
+
 libgearman_server_libgearman_server_la_SOURCES+= libgearman-server/plugins/queue/postgres/queue.cc
-libgearman_server_libgearman_server_la_LIBADD+= $(LTLIBPQ)
+libgearman_server_libgearman_server_la_LIBADD+= $(POSTGRESQL_LDFLAGS)
+
 endif
 

=== modified file 'libgearman-server/plugins/queue/postgres/queue.cc'
--- libgearman-server/plugins/queue/postgres/queue.cc	2011-06-22 04:02:56 +0000
+++ libgearman-server/plugins/queue/postgres/queue.cc	2012-02-05 05:46:18 +0000
@@ -17,13 +17,9 @@
 #include <libgearman-server/plugins/queue/postgres/queue.h>
 #include <libgearman-server/plugins/queue/base.h>
 
-#if defined(HAVE_LIBPQ_FE_H)
-# include <libpq-fe.h>
-# include <pg_config_manual.h>
-#else
-# include <postgresql/libpq-fe.h>
-# include <postgresql/pg_config_manual.h>
-#endif
+#include <postgresql/libpq-fe.h>
+#include <postgresql/pg_config_manual.h>
+
 
 /**
  * @addtogroup plugins::queue::Postgresatic Static libpq Queue Storage Definitions
@@ -194,7 +190,7 @@
     result= PQexec(queue->con, queue->create().c_str());
     if (result == NULL || PQresultStatus(result) != PGRES_COMMAND_OK)
     {
-      gearmand_log_error(GEARMAN_DEFAULT_LOG_PARAM, 
+      gearmand_log_error(GEARMAN_DEFAULT_LOG_PARAM,
 			 "PQexec:%s", PQerrorMessage(queue->con));
       PQclear(result);
       gearman_server_set_queue(server, NULL, NULL, NULL, NULL, NULL);
@@ -243,7 +239,7 @@
     (char *)data,
     (char *)when };
 
-  int param_lengths[]= { 
+  int param_lengths[]= {
     (int)strlen(buffer),
     (int)unique_size,
     (int)function_name_size,
@@ -254,7 +250,7 @@
 
   gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM, "libpq add: %.*s", (uint32_t)unique_size, (char *)unique);
 
-  PGresult *result= PQexecParams(queue->con, queue->insert().c_str(), 
+  PGresult *result= PQexecParams(queue->con, queue->insert().c_str(),
                                  gearmand_array_size(param_lengths),
                                  NULL, param_values, param_lengths, param_formats, 0);
   if (result == NULL || PQresultStatus(result) != PGRES_COMMAND_OK)

=== modified file 'libtest/has.cc'
--- libtest/has.cc	2011-11-07 07:07:01 +0000
+++ libtest/has.cc	2012-02-05 05:46:18 +0000
@@ -1,5 +1,5 @@
 /*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
- * 
+ *
  *  libtest
  *
  *  Copyright (C) 2011 Data Differential, http://datadifferential.com/
@@ -41,3 +41,13 @@
 
   return false;
 }
+
+bool has_postgres_support(void)
+{
+  if (HAVE_LIBPQ)
+  {
+    return true;
+  }
+
+  return false;
+}

=== modified file 'libtest/has.hpp'
--- libtest/has.hpp	2011-11-07 07:07:01 +0000
+++ libtest/has.hpp	2012-02-05 05:46:18 +0000
@@ -1,5 +1,5 @@
 /*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
- * 
+ *
  *  libtest
  *
  *  Copyright (C) 2011 Data Differential, http://datadifferential.com/
@@ -24,3 +24,5 @@
 bool has_memcached_support(void);
 
 bool has_drizzle_support(void);
+
+bool has_postgres_support(void);

=== modified file 'm4/have_libpq.m4'
--- m4/have_libpq.m4	2012-01-18 06:28:31 +0000
+++ m4/have_libpq.m4	2012-02-05 05:46:18 +0000
@@ -3,13 +3,17 @@
 AC_DEFUN([_SEARCH_LIBPQ],[
   AC_REQUIRE([AX_LIB_POSTGRESQL])
 
-  AS_IF([test "x$found_postgresql" = "xyes"],[ ],
+  AS_IF([test "x$found_postgresql" = "xyes"],
+    [
+      AC_DEFINE([HAVE_LIBPQ], [ 1 ], [Enable libpq support])
+    ],
     [
     AC_DEFINE([HAVE_POSTGRESQL], [0], [If libpq is available])
+    AC_DEFINE([HAVE_LIBPQ], [ 0 ], [Enable libpq support])
     ac_cv_libpq="no"
     ])
 
-  AM_CONDITIONAL(HAVE_LIBPQ, [test "x$ac_cv_lib_pq_main" = "xyes"])
+  AM_CONDITIONAL(HAVE_LIBPQ, [test "x$ac_cv_libpq" = "xyes"])
   ])
 
 AC_DEFUN([AX_HAVE_LIBPQ],[

=== modified file 'tests/ports.h'
--- tests/ports.h	2012-01-09 16:27:14 +0000
+++ tests/ports.h	2012-02-05 05:46:18 +0000
@@ -1,5 +1,5 @@
 /*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
- * 
+ *
  *  Gearmand client and server library.
  *
  *  Copyright (C) 2011 Data Differential, http://datadifferential.com/
@@ -55,4 +55,5 @@
 #define EPHEMERAL_PORT GEARMAN_BASE_TEST_PORT +14
 #define REDIS_PORT GEARMAN_BASE_TEST_PORT +15
 #define HTTPD_PORT GEARMAN_BASE_TEST_PORT +16
+#define POSTGRES_TEST_PORT GEARMAN_BASE_TEST_PORT +17
 #define GEARMAN_MAX_TEST_PORT HTTPD_PORT

=== modified file 'tests/postgres.am'
--- tests/postgres.am	2012-01-09 16:27:14 +0000
+++ tests/postgres.am	2012-02-05 05:46:18 +0000
@@ -10,9 +10,18 @@
 # All paths should be given relative to the root
 #
 
-test-postgres: tests-postgres-args
-
-tests-postgres-args: gearmand/gearmand
-if HAVE_LIBPQ
-	@gearmand/gearmand --check-args --libpq-conninfo="foo" --libpq-table="gearmand"
-endif
+test-postgres: tests/postgres_test
+	@tests/postgres_test
+
+gdb-postgres: tests/postgres_test
+	@$(GDB_COMMAND) tests/postgres_test
+
+tests_postgres_test_SOURCES= \
+			    tests/basic.cc \
+			    tests/postgres_test.cc
+tests_postgres_test_CXXFLAGS=
+tests_postgres_test_DEPENDENCIES= ${CLIENT_LDADD} gearmand/gearmand
+tests_postgres_test_LDADD= ${CLIENT_LDADD}
+
+check_PROGRAMS += tests/postgres_test
+noinst_PROGRAMS += tests/postgres_test

=== added file 'tests/postgres_test.cc'
--- tests/postgres_test.cc	1970-01-01 00:00:00 +0000
+++ tests/postgres_test.cc	2012-02-05 05:46:18 +0000
@@ -0,0 +1,122 @@
+/* Gearman server and library
+ * Copyright (C) 2008 Brian Aker, Eric Day
+ * All rights reserved.
+ *
+ * Use and distribution licensed under the BSD license.  See
+ * the COPYING file in the parent directory for full text.
+ */
+
+#include <config.h>
+#include <libtest/test.hpp>
+
+using namespace libtest;
+
+#include <cassert>
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+#include <unistd.h>
+
+#include <libgearman/gearman.h>
+
+#include <tests/basic.h>
+#include <tests/context.h>
+
+#include <tests/ports.h>
+
+#ifndef __INTEL_COMPILER
+#pragma GCC diagnostic ignored "-Wold-style-cast"
+#endif
+
+static test_return_t gearmand_basic_option_test(void *)
+{
+  const char *args[]= { "--check-args",
+    "--queue-type=Postgres",
+    "--libpq-conninfo=\"host=localhost port=5432 user=root dbname=gearman\"",
+    "--libpq-table=gearman",
+    0 };
+
+  test_compare(EXIT_SUCCESS, exec_cmdline(gearmand_binary(), args, true));
+
+  return TEST_SUCCESS;
+}
+
+static test_return_t collection_init(void *object)
+{
+  const char *argv[]= { "test_gearmand",
+    "--queue-type=Postgres",
+    "--libpq-conninfo=\"host=localhost port=5432 user=root dbname=gearman\"",
+    "--libpq-table=gearman",
+    0 };
+
+  Context *test= (Context *)object;
+  assert(test);
+
+  test_truth(test->initialize(4, argv));
+
+  return TEST_SUCCESS;
+}
+
+static test_return_t collection_cleanup(void *object)
+{
+  Context *test= (Context *)object;
+  test->reset();
+
+  return TEST_SUCCESS;
+}
+
+
+static void *world_create(server_startup_st& servers, test_return_t& error)
+{
+  if (has_postgres_support() == false)
+  {
+    error= TEST_SKIPPED;
+    return NULL;
+  }
+
+  Context *test= new Context(POSTGRES_TEST_PORT, servers);
+  if (not test)
+  {
+    error= TEST_MEMORY_ALLOCATION_FAILURE;
+    return NULL;
+  }
+
+  return test;
+}
+
+static bool world_destroy(void *object)
+{
+  Context *test= (Context *)object;
+
+  delete test;
+
+  return TEST_SUCCESS;
+}
+
+test_st gearmand_basic_option_tests[] ={
+  {"all options", 0, gearmand_basic_option_test },
+  {0, 0, 0}
+};
+
+test_st tests[] ={
+  {"gearman_client_echo()", 0, client_echo_test },
+  {"gearman_client_echo() fail", 0, client_echo_fail_test },
+  {"gearman_worker_echo()", 0, worker_echo_test },
+  {"clean", 0, queue_clean },
+  {"add", 0, queue_add },
+  {"worker", 0, queue_worker },
+  {0, 0, 0}
+};
+
+collection_st collection[] ={
+  {"gearmand options", 0, 0, gearmand_basic_option_tests},
+  {"postgres queue", collection_init, collection_cleanup, tests},
+  {0, 0, 0, 0}
+};
+
+void get_world(Framework *world)
+{
+  world->collections= collection;
+  world->_create= world_create;
+  world->_destroy= world_destroy;
+}

