Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access a table within a schema without having to use the schema prefix (Postgres + PHP)?

Tags:

php

postgresql

I have some tables created under a schema. At the moment, I can access the table within the schema using:

select * from myschema.mytable

I am searching for ways to avoid using the schema name. something like:

select * from mytable

But I'm not finding the answers.

Can you help? Thanks

like image 222
mrjayviper Avatar asked Nov 04 '14 00:11

mrjayviper


People also ask

How do I go into a schema in PostgreSQL?

You can access tables inside schemas in two ways: by referencing them using schema. table notation. by adding the schema to your search_path.

What is the difference between schema and database in PostgreSQL?

The same object name can be used in different schemas without conflict; for example, both schema1 and myschema can contain tables named mytable . Unlike databases, schemas are not rigidly separated: a user can access objects in any of the schemas in the database they are connected to, if they have privileges to do so.

How do I connect to a Postgres database?

In order to connect to a database you need to know the name of your target database, the host name and port number of the server, and what user name you want to connect as. psql can be told about those parameters via command line options, namely -d, -h, -p, and -U respectively.


1 Answers

Schema search path would work for this:

To see the current value:

SHOW search_path;

To change the value (append new schema):

SET search_path TO myschema,public;

It is documented here, in section 5.7.3

like image 194
Anti Veeranna Avatar answered Nov 02 '22 06:11

Anti Veeranna