On SQlite I could do a query for PRAGMA user_version; and I could set the version if need be too. Is there anything in postgres that could do the same thing?
I tried select version() but that gets the literal version of postgres, and not a custom set version.
As an update: I researched commenting on databases. Perhaps this could be solution... commenting docs
You can set a custom configuration parameter. The parameter name must contain a dot, e.g.:
set my.version to 4;
select current_setting('my.version') as version;
version
---------
4
(1 row)
A parameter defined in this way is local to the current session. If you want to define a default value for the parameter for all sessions you can add it to the configuration file postgresql.conf (for all databases in the server). Alternatively, it is possible to set the default value for a database in the command:
set my.version to 4;
alter database my_database set my.version from current;
See also:
Setting Parameters
Customized Options
ALTER DATABASE
Related
I am new to databases and I was trying to set up PostreSQL and trying to query
tables in pgAdmin 4, but I always have to specify schema name as such:
SELECT * FROM infection_database.country
The database name is the same as the schema name. This is probably not good but I didn't know better.
I tried looking up some solutions and found and tried this, but it didn't fix the issue:
ALTER DATABASE infection_database SET search_path="infection_database";
Tried both with and without quotation marks if it matters. Where am I making a mistake?
Given solution works, but you need to restart the session for the changes to take effect.
Credit to #jjanes
I am introducing spring to the existing application (hibernate has already been there) and encountered a problem with native SQL queries.
A sample query:
SELECT ST_MAKEPOINT(cast(longitude as float), cast(latitude as float)) FROM
OUR_TABLE;
OUR_TABLE is in OUR_SCHEMA.
When we connect to the db to OUR_SCHEMA:
spring.datasource.url: jdbc:postgresql://host:port/db_name?currentSchema=OUR_SCHEMA
the query fails because function ST_MAKEPOINT is not found - the function is located in schema: PUBLIC.
When we connect to the db without specifying the schema, ST_MAKEPOINT is found and runs correctly, though schema name needs to be added to the table name in the query.
As we are talking about thousands of such queries and all the tables are located in OUR_SCHEMA, is there a chance to anyhow specify the default schema, so still functions from PUBLIC schema were visible?
So far, I have tried the following springboot properties - with no success:
spring.jpa.properties.hibernate.default_schema: OUR_SCHEMA
spring.datasource.tomcat.initSQL: ALTER SESSION SET CURRENT_SCHEMA=OUR_SCHEMA
spring.datasource.initSQL: ALTER SESSION SET CURRENT_SCHEMA=OUR_SCHEMA
Also, it worked before switching to springboot config - specifying hibernate.default-schema = OUR_SCHEMA in persistence.xml was enough.
Stack:
spring-boot: 2.0.6
hibernate: 5.3.1.Final
postgresql: 42.2.5
postgis: 2.2.1
You're probably looking for the PostgreSQL search_path variable, which controls which schemas are checked when trying to resolve database object names. The path accepts several schema names, which are checked in order. So you can use the following
SET search_path=our_schema,public;
This will make PostgreSQL look for your tables (and functions!) first in our_schema, and then in public. Your JDBC driver may or may not support multiple schemas in its current_schema parameter.
Another option is to install the PostGIS extension (which provides the make_point() function) in the our_schema schema:
CREATE EXTENSION postgis SCHEMA our_schema;
This way you only have to have one schema in your search path.
JDBC param currentSchema explicitly allows specifying several schemas separating them by commas:
jdbc:postgresql://postgres-cert:5432/db?currentSchema=my,public&connectTimeout=4&ApplicationName=my-app
From https://jdbc.postgresql.org/documentation/head/connect.html
currentSchema = String
Specify the schema (or several schema separated by commas) to be set in the search-path. This schema will be used to resolve unqualified object names used in statements over this connection.
Note you probably need Postgres 9.6 or better for currentSchema support.
PS Probably better solution is to set search_path per user:
ALTER USER myuser SET search_path TO mydb,pg_catalog;
if you use hibernate.default_schema, then for native queries, you need to provide the {h-schema} placeholder, something like that
SELECT ST_MAKEPOINT(cast(longitude as float), cast(latitude as float)) FROM {h-schema}OUR_TABLE;
Encountered this issue when trying to modify the search_path to my new Redshift db.
Presently, I've migrated the contents of my MySQL db into a redshift cluster via AWS' Data Migration Service. The data was imported into a schema lets call my_schema. When I try to execute queries against the cluster it requires me to prefix table names with the schema name
i.e.
select * from my_schema.my_table
I wanted to change the setup so that I can reference the table directly without needing the prefix. After a bit of looking around I found out that this was possible by modifying the search_path attribute.
First I tried doing this by running
set search_path = "$user", my_schema;
This appeared to work but then I realized that this was simply setting my_schema as the default schema in the context of the current session, I wanted it set on a database level. I found several sources saying that the way to do this was to use the alter command like so...
alter database my_db set search_path = "$user", public, my_schema
However, running this command results in the following error which somehow shows up in 0 google results:
SET/RESET commmand in ALTER DATABASE is not supported
I'm pretty baffled by how the above error hasn't ever had a post made about it but I'm also pretty interested in figuring out how to resolve my initial issue of setting a global default schema for my redshift cluster.
ALTER DATABASE SET is not supported in Redshift. However you can SET/RESET configuration parameters at USER level using the ALTER USER SET SEARCH_PATH TO <SCHEMA1>,<SCHMEA2>;
Please check: http://docs.aws.amazon.com/redshift/latest/dg/r_ALTER_USER.html
http://docs.aws.amazon.com/redshift/latest/dg/r_search_path.html
When you set the search_path to <SCHEMA1>,<SCHMEA2> in db1 for a user it is not for just current session, it will be set for all future sessions.
when i use (pg_dump -f backup.sql mydb), the schema name are not included. How to dump that will output also the scheme name(personal). see example below
Output : ALTER TYPE basicinfo OWNER TO postgres;
// note : basicinfo is the name of the table
Expected Output : ALTER TYPE personal.basicinfo OWNER TO postgres;
any ideas?
Global Database properties such as OWNER etc. are always given with SCHEMA names in a pg_dump output.
However, for non-global database objects, as far as I know, there isn't any way to get SCHEMA names prepended to all the database objects. The way the script works is that it sets the SET search_path before-hand all Schema specific database objects... which is more efficient and has the same effect.
In case you are trying to parse an pg_dump output to extract a given SQL line that works independently, you may have to also parse the nearest-preceding SET search_path line and execute that before executing the target line (for e.g. ALTER TYPE) to have the desired effect.
You cant do that using pg_dump directly.
Try to use different tools, They have different implementation for exporting PostgreSQL database.
Here are list of tools:
phpPgAdmin - http://sourceforge.net/projects/phppgadmin
AnySQL Maestro - http://www.sqlmaestro.com/products/anysql/maestro/
DBeaver - http://dbeaver.jkiss.org/
All refence: https://wiki.postgresql.org/wiki/Community_Guide_to_PostgreSQL_GUI_Tools
I want to create a test environment where the basic underlying postgres database is overlain with an instance-localized private view, such that all queries from a specific set of processes go through the private view while other (potentially concurrent or merely subsequent) processes would remain unaffected.
I think I can do something like this using the search_path mechanism, but it's not clear if I can do that transparently (e.g., without having each application execute some set of SQL setup for each connection). For example, is there something I could set as an environment variable saying "use this search_path" and have every process that I start thereafter see that and use the same private table instances?
If it matters, the processes are all going through the C++ adapter, libpqxx, to access the database.
Thanks,
Jeff
If every instance has a separate database user role, you can simply create a schema with the same name as the user and it'll use it -- without any change to configuration:
myuser=> show search_path;
search_path
--------------
"$user",public
(1 row)
myuser=> create schema myuser;
CREATE SCHEMA
myuser=> create table foo(i int);
CREATE TABLE
myuser=> \d foo
Table "myuser.foo"
Column Type Modifiers
------ ------- ---------
i integer
If you want to have different names for users and schemas, you can configure it for each user manually:
ALTER USER foo SET search_path=foo_schema;
You can configure the default search path for all connections in the postgre configuration file.
See http://www.postgresql.org/docs/9.0/static/runtime-config-client.html#RUNTIME-CONFIG-CLIENT-OTHER
If each connection needs some custom search path based on who the user is you will have to do that in your code and issue a SET search_path TO x,y,z;for each connection.
Another option that comes to mind is using stored functions and have them use dynamic sql to query from different schemas based on who the caller is. You would have to maintain a table or the more evil of the two "hard code" the user/schema mappings into the stored functions that the stored function would use.