Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Alias for PostgreSQL Table

I have a table called assignments. I would like to be able to read/write to all the columns in this table using either assignments.column or homework.column, how can I do this?

I know this is not something you would normally do. I need to be able to do this to provide backwards compatibility for a short period of time.

We have an iOS app that currently does direct postgresql queries against the DB. We're updating all of our apps to use an API. In the process of building the API the developer decided to change the name of the tables because we (foolishly) thought we didn't need backwards compatibility.

Now, V1.0 and the API both need to be able to write to this table so I don't have to do some voodoo later to transfer/combine data later... We're using Ruby on Rails for the API.

like image 603
Phillip Boushy Avatar asked Apr 25 '14 18:04

Phillip Boushy


People also ask

How do I create an alias in PostgreSQL?

Column AliasSELECT column_name AS alias_name FROM table; or, SELECT column_name alias_name FROM table; Below Syntax is for column alias used with expressions: SELECT expression alias_name FROM table; The primary use of column alias is to make the output of a query more meaningful.

Can we use alias in PostgreSQL?

In PostgreSQL, an alias is a temporary alternative name for columns, tables, views, materialized views, etc. in a query. Aliases are assigned during query execution and aren't stored in the database or on disk. By using column aliases, the query output can become more meaningful.

How do I specify a column alias?

To create column aliases: Type: SELECT column1 [AS] alias1, column2 [AS] alias2, ... columnN [AS] aliasN FROM table; column1, column2, ..., columnN are column names; alias1, alias2, ..., aliasN are their corresponding column aliases; and table is the name of the table that contains column1, column2, ....

How do I change a table name in PostgreSQL?

To rename a table, the PostgreSQL ALTER TABLE syntax is: ALTER TABLE table_name RENAME TO new_table_name; table_name. The table to rename.


1 Answers

With Postgres 9.3 the following should be enough:

CREATE VIEW homework AS SELECT * FROM assignments;

It works because simple views are automatically updatable (see docs).

like image 58
hegemon Avatar answered Sep 21 '22 16:09

hegemon