Postgresql Error: 42P01: Relation "[Table]" Does Not Exist

PostgreSQL ERROR: 42P01: relation [Table] does not exist

you have two choices:
- no quotes: then everything will automatically be lowercase and non-case-sensitive
- with quotes: from now on everything is case sensitive.

i would highly recommend to NOT use quotes and make PostgreSQL behave non case sensitive. it makes life so much easier. once you get into quoting you got to use it EVERYWHERE as PostgreSQL will start to be very precise.

some example:

   TEST = test       <-- non case sensitive
"Test" <> Test <-- first is precise, second one is turned to lower case
"Test" = "Test" <-- will work
"test" = TEST <-- should work; but you are just lucky.

really try to avoid this kind of trickery at any cost. stay with 7 bit ascii for object names.

“Error 42P01: relation does not exist” (non public schema in PostgreSQL)

I had the same issue with adding foreign keys to a table in a custom schema from the modify table window. Adding schema to the "Target table" field did not work either. Instead I selected "Open in editor" rather than "Execute in database" and added the schema to the table name in the generated statement and it ran fine.

Why PSQL create table returns [42P01] ERROR: relation does not exist

The script is run sequentially. So when the pagecolours table is created, the users table does not yet exist and thus the references users fails.

You need to re-order the script, so that the users table is created first. But as you have a circular reference (users references pagecolours and pagecolours references users) you need to create the tables without an "inline" reference, and then at the end of the script you need to run an ALTER TABLE to create the foreign keys.

But having a circular reference like that, is usually not a good idea. But if you are 100% sure you need it, you should at least declare the foreign keys as deferrable, to make inserting rows easier.


Also: serial is not a datatype. A foreign key column that references a serial column should be defined as an integer. In general it is recommended to move away from serial and use integer generated always as identity instead.

ERROR: permission denied for relation tablename on Postgres while trying a SELECT as a readonly user

Here is the complete solution for PostgreSQL 9+, updated recently.

CREATE USER readonly  WITH ENCRYPTED PASSWORD 'readonly';
GRANT USAGE ON SCHEMA public to readonly;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO readonly;

-- repeat code below for each database:

GRANT CONNECT ON DATABASE foo to readonly;
\c foo
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO readonly; --- this grants privileges on new tables generated in new database "foo"
GRANT USAGE ON SCHEMA public to readonly;
GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO readonly;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO readonly;

Thanks to https://jamie.curle.io/creating-a-read-only-user-in-postgres/ for several important aspects

If anyone find shorter code, and preferably one that is able to perform this for all existing databases, extra kudos.

EF Core - Table '*.__EFMigrationsHistory' doesn't exist

Turning Mark G's comment into an answer.

Once the __EFMigrationsHistory table has been created, the rest of the update should run.

CREATE TABLE `__EFMigrationsHistory` ( `MigrationId` nvarchar(150) NOT NULL, `ProductVersion` nvarchar(32) NOT NULL, PRIMARY KEY (`MigrationId`) );

Alternatively, generate the script of your migration(s) and apply to the database manually using this command in Package Manager Console:

Script-Migration

If you need to generate All scripts, you can use this command:

Script-Migration -from 0


Related Topics



Leave a reply



Submit