Skip to content

Commit

Permalink
osm2pgsql: Add authentication options (user, host, port, password).
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.openstreetmap.org/applications/utils/export/osm2pgsql@6920 b9d5c4c9-76e1-0310-9c85-f3177eceb1e4
  • Loading branch information
jonb committed Feb 18, 2008
1 parent 7595f8e commit 5929e54
Show file tree
Hide file tree
Showing 6 changed files with 273 additions and 16 deletions.
7 changes: 1 addition & 6 deletions middle-pgsql.c
Expand Up @@ -20,9 +20,6 @@

#include "output-pgsql.h"

/* Postgres database parameters */
static char conninfo[256];

enum table_id {
t_node, t_node_tag, t_way_tag, t_way_nd
} ;
Expand Down Expand Up @@ -511,15 +508,13 @@ static void pgsql_end(void)
}

#define __unused __attribute__ ((unused))
static int pgsql_start(const char *db, int latlong __unused)
static int pgsql_start(const char *conninfo, int latlong __unused)
{
char sql[2048];
PGresult *res;
int i;
int dropcreate = 1;

snprintf(conninfo, sizeof(conninfo), "dbname = %s", db);

/* We use a connection per table to enable the use of COPY */
sql_conns = calloc(num_tables, sizeof(PGconn *));
assert(sql_conns);
Expand Down
3 changes: 2 additions & 1 deletion middle-ram.c
Expand Up @@ -192,7 +192,8 @@ static int ram_ways_set(int id, struct keyval *nds, struct keyval *tags)
if (!polygon) {
struct osmNode *nodes = getNodes(ndids, &ndCount);
if (nodes) {
out_pgsql.way(id, tags, nodes, ndCount);
if (ndCount)
out_pgsql.way(id, tags, nodes, ndCount);
free(nodes);
}
free(ndids);
Expand Down
72 changes: 69 additions & 3 deletions osm2pgsql.c
Expand Up @@ -31,6 +31,8 @@
#include <assert.h>
#include <getopt.h>

#include <libpq-fe.h>

#include <libxml/xmlstring.h>
#include <libxml/xmlreader.h>

Expand All @@ -44,6 +46,7 @@
#include "reprojection.h"
#include "text-tree.h"
#include "input.h"
#include "sprompt.h"

static int count_node, max_node;
static int count_way, max_way;
Expand Down Expand Up @@ -334,6 +337,10 @@ static void usage(const char *arg0)
fprintf(stderr, " -s|--slim\t\tStore temporary data in the database. This greatly\n");
fprintf(stderr, " \t\treduces the RAM usage but is much slower.\n");
#endif
fprintf(stderr, " -U|--username\tPostgresql user name.\n");
fprintf(stderr, " -W|--password\tForce password prompt.\n");
fprintf(stderr, " -H|--host\t\tDatabase server hostname or socket location.\n");
fprintf(stderr, " -P|--port\t\tDatabase server port.\n");
fprintf(stderr, " -h|--help\t\tHelp information.\n");
fprintf(stderr, " -v|--verbose\t\tVerbose output.\n");
fprintf(stderr, "\n");
Expand All @@ -350,15 +357,55 @@ static void usage(const char *arg0)
}
}

const char *build_conninfo(const char *db, const char *username, const char *password, const char *host, const char *port)
{
static char conninfo[1024];

conninfo[0]='\0';
strcat(conninfo, "dbname='");
strcat(conninfo, db);
strcat(conninfo, "'");

if (username) {
strcat(conninfo, " user='");
strcat(conninfo, username);
strcat(conninfo, "'");
}
if (password) {
strcat(conninfo, " password='");
strcat(conninfo, password);
strcat(conninfo, "'");
}
if (host) {
strcat(conninfo, " host='");
strcat(conninfo, host);
strcat(conninfo, "'");
}
if (port) {
strcat(conninfo, " port='");
strcat(conninfo, port);
strcat(conninfo, "'");
}

return conninfo;
}

int main(int argc, char *argv[])
{
int append=0;
int create=0;
int slim=0;
int sanitize=0;
int pass_prompt=0;
int latlong = 0, sphere_merc = 0;
const char *db = "gis";
const char *username=NULL;
const char *host=NULL;
const char *password=NULL;
const char *port = "5432";
const char *conninfo = NULL;
const char *prefix = "planet_osm";
PGconn *sql_conn;

fprintf(stderr, "osm2pgsql SVN version %s $Rev$ \n\n", VERSION);

Expand All @@ -377,11 +424,15 @@ int main(int argc, char *argv[])
{"prefix", 1, 0, 'p'},
{"merc", 0, 0, 'm'},
{"utf8-sanitize", 0, 0, 'u'},
{"username", 1, 0, 'U'},
{"password", 0, 0, 'W'},
{"host", 1, 0, 'H'},
{"port", 1, 0, 'P'},
{"help", 0, 0, 'h'},
{0, 0, 0, 0}
};

c = getopt_long (argc, argv, "ab:cd:hlmp:suv", long_options, &option_index);
c = getopt_long (argc, argv, "ab:cd:hlmp:suvU:WH:P:", long_options, &option_index);
if (c == -1)
break;

Expand All @@ -398,6 +449,10 @@ int main(int argc, char *argv[])
case 'm': sphere_merc=1; break;
case 'p': prefix=optarg; break;
case 'd': db=optarg; break;
case 'U': username=optarg; break;
case 'W': pass_prompt=1; break;
case 'H': host=optarg; break;
case 'P': port=optarg; break;

case 'h':
case '?':
Expand All @@ -417,6 +472,17 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE);
}

if (username || pass_prompt)
password = simple_prompt("Password:", 100, 0);

conninfo = build_conninfo(db, username, password, host, port);
sql_conn = PQconnectdb(conninfo);
if (PQstatus(sql_conn) != CONNECTION_OK) {
fprintf(stderr, "Connection to database failed: %s\n", PQerrorMessage(sql_conn));
exit(EXIT_FAILURE);
}
PQfinish(sql_conn);

text_init();
initList(&tags);
initList(&nds);
Expand All @@ -443,11 +509,11 @@ int main(int argc, char *argv[])
mid = slim ? &mid_pgsql : &mid_ram;
out = &out_pgsql;

out->start(db, prefix, append);
out->start(conninfo, prefix, append);

while (optind < argc) {
fprintf(stderr, "\nReading in file: %s\n", argv[optind]);
mid->start(db, latlong);
mid->start(conninfo, latlong);
if (streamFile(argv[optind], sanitize) != 0)
exit_nicely();
mid->end();
Expand Down
7 changes: 1 addition & 6 deletions output-pgsql.c
Expand Up @@ -21,9 +21,6 @@
#include "build_geometry.h"
#include "middle-pgsql.h"

/* Postgres database parameters */
static char conninfo[256];

/* Set if the output is in lat/lon */
#define SRID (project_getprojinfo()->srs)

Expand Down Expand Up @@ -531,14 +528,12 @@ static int pgsql_out_relation(int id, struct keyval *rel_tags, struct osmNode **
return 0;
}

static int pgsql_out_start(const char *db, const char *prefix, int append)
static int pgsql_out_start(const char *conninfo, const char *prefix, int append)
{
char sql[1024], tmp[128];
PGresult *res;
unsigned int i,j;

snprintf(conninfo, sizeof(conninfo), "dbname = %s", db);

/* We use a connection per table to enable the use of COPY_IN */
sql_conns = calloc(num_tables, sizeof(PGconn *));
assert(sql_conns);
Expand Down

0 comments on commit 5929e54

Please sign in to comment.