Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect with postgreSQL schema

I am looking to connect and query to a PostgreSQL. But I only want to connect to a particular Schema.

As per the doc (JDBC) we can use

jdbc:postgresql://localhost:5432/mydatabase?searchpath=myschema

or update As of 9.4 you can specify the url with the new currentSchema parameter like so:

jdbc:postgresql://localhost:5432/mydatabase?currentSchema=myschema

But I am unable to do so with golang SQL driver;

As per the documents, we can also use SET search_path TO myschema,public; But I only want to declare it for once during initializing but I think this needs to be executed every time for new connection.

Also I am using following code please help me identify the correct parameters to be passed to this in order to only connect with schema

db, err := sql.Open("postgres", `dbname=`+s.settings.Database+
` user=`+s.settings.Username+` password=`+s.settings.Password+
` host=`+s.settings.Url+` sslmode=disable`) 

Adding currentSchema=myschema or searchpath=myschema is not working!

Is there a way I can only connect to a particular database-schema in GO

like image 252
Abhishek Soni Avatar asked Jul 21 '18 18:07

Abhishek Soni


People also ask

What are postgres schemas?

In PostgreSQL, schema is a named collection of tables, views, functions, constraints, indexes, sequences etc. PostgreSQL supports having multiple schemas in a single database there by letting you namespace different features into different schemas.


1 Answers

You should add search_path=myschema to dataSourceName

P.S. better use fmt.Sprintf("host=%s port=%d dbname=%s user=%s password='%s' sslmode=disable search_path=%s", ...) instead ``+``

like image 115
Mytholog Avatar answered Oct 13 '22 04:10

Mytholog