Can't select from database without specifying schema name - postgresql

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

Related

Should the table name "user" be used in a Symfony project?

I know this sounds like an "opinion" question, but I don't think it is.
Normally, I would consider not using "user" as a table name as it is a reserved word, and I would rather not bother having to deal with it when I write native SQL queries.
But I am not writing native SQL queries, but am having Symfony and the Doctrine ORM perform all the queries. And when I execute php bin/console make:user, Symfony prompts me to select the class name User which results with user as the table name. Furthermore, most of the tutorials I have read also use the name user. If using the name user is what is most common for other developers, I would rather stay consistent.
Most of the time, I don't have any issues, but every now and then, Doctrine crashes because it is querying the Postgres internal user table and not public.user. As a workaround, I tried adding * #ORM\Table(schema="public") to the User entity, but then when making migrations, it tries to duplicate the record resulting in errors. Maybe Symfony/Doctrine needs to be configured somewhere as using the public schema?
Thanks
Since my question was "Should the table name “user” be used in a Symfony project?", the following doesn't answer the question, but I am still posting it should it be helpful for others. Perhaps I will change the title to "How to use the table name “user” in a Doctrine project?", but not sure whether doing so is kosher.
I've since discovered other's experiencing issues resulting from this topic:
https://github.com/doctrine/dbal/issues/1222
https://github.com/symfony/maker-bundle/pull/545
Also found the following at doctrine-adding-mapping:
Be careful not to use reserved SQL keywords as your table or column
names (e.g. GROUP or USER). See Doctrine’s Reserved SQL keywords
documentation for details on how to escape these. Or, change the table
name with #ORM\Table(name="groups") above the class or configure the
column name with the name="group_name" option.
Which directed me to quoting-reserved-words which directed me to escape the name using ticks.
* #ORM\Table(name="`user`")
There is no table user in PostgreSQL.
Tutorials that use user as a table name are not trustworthy. However, if you consistently use double quotes, there should be no problem. Since you claim to have problems with native queries, you might have forgotten that occasionally.
One possible source of confusion is that there is a function named user in PostgreSQL. So if you use user in a context where a function is possible, you'll get a function call. If you use it with schema qualification, you will get an error that there is no such object. Otherwise, you will get a syntax error:
test=> SELECT user;
user
---------
laurenz
(1 row)
test=> SELECT * FROM user;
user
---------
laurenz
(1 row)
test=> TABLE user;
ERROR: syntax error at or near "user"
LINE 1: TABLE user;
^
test=> SELECT * FROM public.user;
ERROR: relation "public.user" does not exist
LINE 1: SELECT * FROM public.user;
^

PostgreSQL Create Table command not working, checked access privileges and all default privileges are there

I'm extremely new to PostgreSQL and I just installed it using Homebrew.
I ran through creating and connecting to a database and now I'm trying to create a table with the standard command CREATE TABLE users(name string, age smallint, birthday date) and the command completes. However as soon as I run the command to list all tables I get the following Did not find any relations.
I checked all users and privileges and it looks like my profiles roles include Superuser, Create Role, Create DB, Replication, Bypass RLS and I'm a member of {}.
I'm not sure if there's something more I need to do in order to create tables under a certain database or not, but I've looked all over and can't seem to find an answer to this.
I'm not really sure what you're intending to do, be more explicit.
But it seems to me you've juste created tables and did not create any relations.
Try using a key and defining mother tables ?

Default schema for native SQL queries (spring-boot + hibernate + postgresql + postgis)

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;

SET/RESET command in ALTER DATABASE is not supported

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.

Not able to query after Alter table in DB2

We have a table someone created in DB2. I have no idea how they created it. But when I edit the table, It edits just fine. But after edit I can not query the table at all THE COLUMN CANNOT BE ADDED TO THE TABLE BECAUSE THE TABLE HAS AN EDIT PROCEDURE.
I looked ibm site and found this how to edit table using procedure
But I have no idea how to do this.
Is there anything that I can do to fix this with out following the procedure mentioned in second link?
I restarted server, but still no help. First I'm not able to figure out why I get the error in first place.
I'm using DB Visualizer and DB2 on linux.
This is sometimes default behavior of DB2. We need to run reorgchk command to fix these errors. More info below..
http://publib.boulder.ibm.com/infocenter/db2luw/v9/index.jsp?topic=/com.ibm.db2.udb.admin.doc/doc/r0000888.htm
http://publib.boulder.ibm.com/infocenter/db2luw/v9/index.jsp?topic=/com.ibm.db2.udb.admin.doc/doc/c0023297.htm