I’m having this strange problem using PostgreSQL 9.3 with tables that are created using qoutes. For instance, if I create a table using qoutes:
create table "TEST" ("Col1" bigint);
the table is properly created and I can see that the quotes are preserved when view it in the SQL pane of pgAdminIII. But when I query the DB to find the list of all available tables (using the below query), I see that the result does not contain quotes around the table name.
select table_schema, table_name from information_schema.tables where not table_schema='pg_catalog' and not table_schema='information_schema';
Since the table was created with quotes, I can’t use the table name returned from the above query directly since it is unquoted and throws the error in posted in the title.
I could try surrounding the table names with quotes in all queries but I’m not sure if it’ll work all the time. I’m looking for a way to get the list of table names that are quoted with quotes in the result.
I’m having the same issue with column names as well but I’m hoping that if I can find a solution to the table names issue, a similar solution will work for column names as well.
PostgreSQL error 42P01 actually makes users dumbfounded, especially the newbies.
Usually, this error occurs due to an undefined table in newly created databases.
That’s why at Bobcares, we often get requests to fix PostgreSQL errors, as a part of our Server Management Services.
Today, let’s have a look into the PostgreSQL error 42P01 and see how our Support Engineers fix it.
What is PostgreSQL error 42P01?
PostgreSQL has a well-defined error code description. This helps in identifying the reason for the error.
Today, let’s discuss in detail about PostgreSQL error 42P01. The typical error code in PostgreSQL appears as:
ERROR: relation "[Table name]" does not exist
SQL state:42P01
Here the 42P01 denotes an undefined table.
So, the code description clearly specifies the basic reason for the error.
But what does an undefined table means?
Let’s discuss it in detail.
Causes and fixes for the PostgreSQL error 42P01
Customer query on undefined tables of a database often shows up the 42P01 error.
Now let’s see a few situations when our customers get the 42P01 error. We will also see how our Support Engineers fix this error.
1. Improper database setup
Newbies to Postgres often make mistakes while creating a new database. Mostly, this improper setup ends up in a 42P01 error.
In such situations, our Support Team guides them for easy database setup.
Firstly, we create a new database. Next, we create a new schema and role. We give proper privileges to tables.
Postgres also allows users to ALTER DEFAULT PRIVILEGES.
2. Unquoted identifiers
Some customers create tables with mixed-case letters.
Usually, the unquoted identifiers are folded into lowercase. So, when the customer queries the table name with the mixed case it shows 42P01 error.
The happens as the PostgreSQL has saved the table name in lower case.
To resolve this error, our Support Engineers give mixed case table name in quotes. Also, we highly recommend to NOT use quotes in database names. Thus it would make PostgreSQL behave non-case sensitive.
3. Database query on a non-public schema
Similarly, the PostgreSQL 42P01 error occurs when a user queries a non-public schema.
Usually, this error occurs if the user is unaware of the proper Postgres database query.
For instance, the customer query on table name ‘pgtable‘ was:
SELECT * FROM pgtable
This query is totally correct in case of a public schema. But, for a non-public schema ‘xx’ the query must be:
SELECT * FROM "xx"."pgtable"
Hence, our Support Engineers ensure that the query uses the correct schema name.
[Still having trouble in fixing PostgreSQL errors? – We’ll fix it for you.]
Conclusion
In short, PostgreSQL error 42P01 denotes the database query is on an undefined table. This error occurs due to improper database setup, unidentified table name, and so on. Today, we saw how our Support Engineers fix the undefined table error in Postgres.
PREVENT YOUR SERVER FROM CRASHING!
Never again lose customers to poor server speed! Let us help you.
Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.
GET STARTED
var google_conversion_label = «owonCMyG5nEQ0aD71QM»;
What you had originally was a correct syntax — for tables, not for schemas. As you did not have a table (dubbed ‘relation’ in the error message), it threw the not-found error.
I see you’ve already noticed this — I believe there is no better way of learning than to fix our own mistakes 😉
But there is something more. What you are doing above is too much on one hand, and not enough on the other.
Running the script, you
- create a schema
- create a role
- grant
SELECTon all tables in the schema created in (1.) to this new role_ - and, finally, grant all privileges (
CREATEandUSAGE) on the new schema to the new role
The problem lies within point (3.) You granted privileges on tables in replays — but there are no tables in there! There might be some in the future, but at this point the schema is completely empty. This way, the GRANT in (3.) does nothing — this way you are doing too much.
But what about the future tables?
There is a command for covering them: ALTER DEFAULT PRIVILEGES. It applies not only to tables, but:
Currently [as of 9.4], only the privileges for tables (including views and foreign tables), sequences, functions, and types (including domains) can be altered.
There is one important limitation, too:
You can change default privileges only for objects that will be created by yourself or by roles that you are a member of.
This means that a table created by alice, who is neither you nor a role than you are a member of (can be checked, for example, by using du in psql), will not take the prescribed access rights. The optional FOR ROLE clause is used for specifying the ‘table creator’ role you are a member of. In many cases, this implies it is a good idea to create all database objects using the same role — like mydatabase_owner.
A small example to show this at work:
CREATE ROLE test_owner; -- cannot log in
CREATE SCHEMA replays AUTHORIZATION test_owner;
GRANT ALL ON SCHEMA replays TO test_owner;
SET ROLE TO test_owner; -- here we change the context,
-- so that the next statement is issued as the owner role
ALTER DEFAULT PRIVILEGES IN SCHEMA replays GRANT SELECT ON TABLES TO alice;
CREATE TABLE replays.replayer (r_id serial PRIMARY KEY);
RESET ROLE; -- changing the context back to the original role
CREATE TABLE replays.replay_event (re_id serial PRIMARY KEY);
-- and now compare the two
dp replays.replayer
Access privileges
Schema │ Name │ Type │ Access privileges │ Column access privileges
─────────┼──────────┼───────┼───────────────────────────────┼──────────────────────────
replays │ replayer │ table │ alice=r/test_owner ↵│
│ │ │ test_owner=arwdDxt/test_owner │
dp replays.replay_event
Access privileges
Schema │ Name │ Type │ Access privileges │ Column access privileges
─────────┼──────────────┼───────┼───────────────────┼──────────────────────────
replays │ replay_event │ table │ │
As you can see, alice has no explicit rights on the latter table. (In this case, she can still SELECT from the table, being a member of the public pseudorole, but I didn’t want to clutter the example by revoking the rights from public.)
I’m new to SQL. I was trying to run sql schema, here is a part of code
create table PageColours (
id serial,
userID serial references users(id),
--references users(id),
isTemplate boolean default'false',
name NameValue not null unique,
primary key (id)
);
create table People (
id serial,
email EmailValue not null unique,
givenName NameValue not null,
familyName NameValue,
invitedID serial references Events(id),
attendedID serial references Events(id),
primary key (id)
);
create table users(
id serial
references People(id),
passWord varchar not null,
BillingAddress serial not null
references Places(id),
HomeAddress serial
references Places(id),
ListID serial
references ContactLists(id),
ColorID serial
references PageColours(id),
primary key (id)
);
It returns[2020-07-03 15:28:19] [42P01] ERROR: relation "people" does not exist
[2020-07-03 15:28:19] [42P01] ERROR: relation "users" does not exist
In fact all foreign key reference table reference not exist. When i remove the reference, the table can be created, can someone please help me ?
I’m new to SQL. I was trying to run sql schema, here is a part of code
create table PageColours (
id serial,
userID serial references users(id),
--references users(id),
isTemplate boolean default'false',
name NameValue not null unique,
primary key (id)
);
create table People (
id serial,
email EmailValue not null unique,
givenName NameValue not null,
familyName NameValue,
invitedID serial references Events(id),
attendedID serial references Events(id),
primary key (id)
);
create table users(
id serial
references People(id),
passWord varchar not null,
BillingAddress serial not null
references Places(id),
HomeAddress serial
references Places(id),
ListID serial
references ContactLists(id),
ColorID serial
references PageColours(id),
primary key (id)
);
It returns[2020-07-03 15:28:19] [42P01] ERROR: relation "people" does not exist
[2020-07-03 15:28:19] [42P01] ERROR: relation "users" does not exist
In fact all foreign key reference table reference not exist. When i remove the reference, the table can be created, can someone please help me ?
- Remove From My Forums
-
Question
-
public bool girisKontrol(string user, string pass) {string sunucu, port, kullaniciadi, sifre, veritabani; sunucu = "localhost"; port = "5432"; kullaniciadi = "postgres"; sifre = "tellioglu"; veritabani = "postgres"; string baglantimetni = string.Format("Server ={0};Port ={1};User ID = {2};Password = {3};Database={4};", sunucu, port, kullaniciadi, sifre, veritabani); var baglanti = new NpgsqlConnection(); baglanti.ConnectionString = baglantimetni; var cmd = new NpgsqlCommand(); cmd.Connection = baglanti; cmd.CommandText = "select * from kullanicigiris where Kullaniciadi = @Kullanici and sifre = @sifre";//kullanicigiris tablonun adi , Kullaniciadi sütünun adı,sifre sütunun adi cmd.Parameters.AddWithValue("@Kullanici", user); cmd.Parameters.AddWithValue("@sifre", pass); cmd.Connection.Open(); var reader = cmd.ExecuteReader(); var sonuc = reader.HasRows; reader.Close(); reader.Dispose(); cmd.Connection.Close(); cmd.Connection.Dispose(); cmd.Dispose(); return sonuc; }i am using postgreSQL database . executereader(); giving ‘ERROR: 42P01: relation does not exist’ problem. is sql line wrong i dont know please help me
Answers
-
From the code sinppet, I don’t think it’s the SQL line error. In C#, we should use the SQL parameters like yours.
But for postgreSQL, I would suggest you to try the following code to see if it works.
cmd.CommandText = "select * from kullanicigiris where Kullaniciadi = :Kullanici and sifre = :sifre" cmd.Parameters.AddWithValue(":Kullanici", user); cmd.Parameters.AddWithValue(":sifre", pass);And there is a category for postgreSQL support query for your reference:
http://www.postgresql.org/support/
Hope it hleps.
Best Regards,
Rocky Yue[MSFT]
MSDN Community Support | Feedback to us
-
Marked as answer by
Wednesday, May 2, 2012 2:51 AM
-
Marked as answer by
-
PostgreSQL
-
Администрирование баз данных
При подключении к базе данных (postgreSQL) с программы-клиента выдается ошибка 42Р01 отношение «dbo.users» не существует. Что делать и с чем это связано?
-
Вопрос заданболее трёх лет назад
-
3061 просмотр
1
комментарий
-
1. БД не подключена, проверьте.
Решения вопроса 1
![]()
-
Я так и не понял откуда удалить надо, в postgresql все вроде норм
Пригласить эксперта
Похожие вопросы
-
Показать ещё
Загружается…
29 янв. 2023, в 03:07
300000 руб./за проект
29 янв. 2023, в 02:16
700000 руб./за проект
29 янв. 2023, в 01:54
5000 руб./за проект