I have a Postgres DB that I need to modify. It is in my localhost. I am trying to use Node Postgres for it, but I am missing something out from the document.
const client = new Client({
user: 'postgreadmin',
host: 'localhost',
database: 'testdb',
password: 'somepassword',
port: 5433,
})
client.query('SELECT * FROM User', function(err, result) {
console.log
(result);
client.end();
});
The result shows return 'postgreadmin' as the user. Which is somewhat confusing. My database users should contain users like 'peter', 'mary' etc. The query seems to be running on the Postgres' configuration, and not on the db tables itself.
I tried other tables and the error is "no relation Merchant found'. I am not sure how to connect to the tables. My tables are under the schema call 'public'. So my question is do i need to specify the in pg's configuration to connect to the schema 'public'?
user is a function that returns the current database user, so select user will return the database user (and select * from user will as well).
If you did create a table with that name you need to use double quotes:
SELECT * FROM "User"
Note that quoted identifiers are case-sensitive. "User" is a different name than "user" or "USER". Check your database for the correct spelling. The same is probably true for "Merchant"
I strongly recommend you rename the table to something that is not a reserved keyword and does not collide with any internal function name.
For more details, see the manual:
https://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
Related
I am trying to drop the schema masterdata from a postgres database, but it does not work. I am using PostgreSQL 9.5. I have 2 databses, one of them has the masterdata schema, which I want to drop. Here is the structure (in DBeaver):
My first attempt was just to execute the SQL statement DROP SCHEMA masterdata in DBeaver, but it tells me, that such a schema does not exist (but it does show it, as we can see in the picture!). Maybe, it does not know, in which of the 2 databses to look for this schema? If so, then how to specify it? I was looking for a way to specify the database, but did not find anything.
However, my second attempt was to use psql and to type the command
psql -U postgres -d bobd -h localhost
So here I concretely specify, which databse to use! psql does not answer anything, it just asks for the next command. And when I type \dn to view the current schemas, then the schema masterdata is still there! Also, the data in the tables is still there. I tried the same with other users instead of the user postgres (also with the owner of the schema to remove), but the result is the same.
Any ideas, what I am doing wrong?
Your DBeaver session was probably connected to the wrong database (postgres?).
Do it from the psql session, that is easiest.
Right after \dn showed you that there is indeed such a schema, enter
DROP SCHEMA masterdata;
It may complain that there are objects in that schema. Then you can use
DROP SCHEMA masterdata CASCADE;
Maybe your schema name contains characters that do not display or display differently (maybe capital letters) in DBeaver. I suggest you check names by running following query:
SELECT '~~' || nspname || '~~' FROM pg_catalog.pg_namespace;
Try adding quotes around schema name if you have added capital letters when creating schema.
Simple question, however google can't help in reasonable time.
Ok, I have user table in my_db database with id column.
I want to run very simple query
SELECT id FROM user;
but it fails.
ERROR: column "id" does not exist LINE 1: SELECT id FROM user;
Can you imagine?
Ok, Running
SELECT * FROM user;
outputs the list of internal postgresql database users, which is nothing to do with my users, it's data from completely another [internal] database.
However, connection with my_db was established.
user is an internal function (and a reserved word) returning the currently logged in user.
To use that as your own identifier, you need to quote it:
select id
from "user"
or
select id
from public."user".
But you should really avoid reserved words as table names (or any name that requires quoting the identifier)
The following query can be rewritten as
SELECT id FROM my_db.public.user;
Where id is column, my_db is database, user is table name, public - is the schema. More about schemas:
http://www.postgresql.org/docs/9.1/static/ddl-schemas.html
So you don't have to rename the table name.
grails with postgres for User domain.
Message: ERROR: column this_.id does not exist
Got the issue.
For User domain, I've postgres table as "user". So by default when it is trying to query user table, its not querying with "user.id". There is something wrong with postgres for "user" table.
So I updated my "user" table to "myapp_user" table. The problem got solved.
The word 'user' may be reserved by dbms.
static mapping = {
table '`User`'
password column: '`password`'
}
I think it's because table name user already occupied by defalut by Postgres. Try query w/ schema (public by default) like in phpPgAdmin:
SELECT * FROM "public"."user"
THis doesn't work, resulting in ERROR: relation "user" does not exist
select * from "user"
This one does
select * from "dbo"."user"
Tables that aren't schema-qualified are searched for on the search_path. This doesn't search every schema. By default it only searches:
pg_catalog (implicitly always first, not listed on search_path)
pg_temp tablespaces (implicit, not listed on search_path)
A schema named after the current user, listed as $user in search_path
The public schema
If you want PostgreSQL to look elsewhere you have to modify the search_path to tell it so, or fully schema-qualify the name.
See the PostgreSQL manual on search_path.
Also, note that unlike some databases the name dbo has no particular significance in PostgreSQL. It's just another name.
I m getting some problem while using postgres_fdw.
CREATE SERVER foreign_server
FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (host '192.162.0.1', port '5432', dbname 'Test');
CREATE USER MAPPING FOR postgres
SERVER foreign_server
OPTIONS (user 'foreign_user', password 'password');
CREATE FOREIGN TABLE foreign_table (
id serial NOT NULL,
data text)SERVER foreign_server
OPTIONS (schema_name 'public', table_name 'employee');
select * from employee where user ='foreign_user'
Now I can see entries are made to pg_foreign_data_wrapper, pg_foreign_server and pg_foreign_table tables.
But how do I access employee table of remote system.
I mean select * from employee where user ='foreign_user' doesn't give any result. Though it has data in Employee table of remote system.
Any idea please?
But How do I access employee table of remote system.
You just need to access the foreign table, say "SELECT * FROM foreign_table;".
The procedure seems fine, but your foreign table doesn't have a column named "user", so your query must cause an error.
It would be better to show what has happened actually. Showing actual query and error messages helps us understand where the problem is.