13

Is there any way to give a postgres database an alias?

Essentially, I need a single database to operate with two names, so that I could make queries to, say, DB_ALPHA and DB_ONE and they'd have the exact same effect. I accomplished this in MySQL by putting a symbolic link to the database in the same directory. I haven't been able to find an equivalent of this solution in postgres.

I appreciate the help! Thanks!

bigmattyh
  • 308
  • 1
  • 2
  • 9

4 Answers4

8

You cannot create an alias to a schema or database since the Schemaname / DBname is used internally as primary key for the tables pg_namespace and pg_database.

pacey
  • 3,833
  • 1
  • 16
  • 31
3

The best solution seems to be PgBouncer. It allows you to pool and "redirect" connections from one database to another (even on a different host).

kontextify
  • 189
  • 1
  • 10
1

No you can't. In PostgreSQL you have to connect to a single database before you can execute any query. In MySQL you have only one database and many schema's (called "databases" in the MySQL-world), you can always execute a query on a different "database".

If you want the same setup in PostgreSQL, just create different schema's in your database.

CREATE SCHEMA foo;
CREATE SCHEMA bar;
Frank Heikens
  • 1,258
  • 6
  • 8
0

Do you need write queries as well as read queries? I'd be tempted to try and create the second database, and then use a symbolic link at the database directory level to point to the first databases directory. That might be ok as long as you were sticking to read only queries in the second system; otoh you might get all kinds of corruption from weird shared memory related problems, but it'd be an interesting experiment.

xzilla
  • 357
  • 1
  • 8