I installed PostgreSQL on my local machine and create the table in my own database.
Database properties looks like
Name: MyDB
Tablespace: pg_default
Default tablespace: pg_default
System database? No
And my table properties looks like
Name: mytable
Tablespace: pg_default
System table? No
But when I try to do
select * from mytable;
I am getting
ERROR: relation "mytable" does not exist
Create table statement
create table mytable (
.....
);
Any suggestion?
I had the same issue, and clearly, if the create statements were including double quotes around the tables names, you have to use them too in your select statement to see the table (granted that you are the proper user with the proper privileges on tables). (and it's case sensitive even in Windows...)
Related
In my database i have a partitioned table with name 'record_partitioned' on PostgreSQL 11.2.
i want to change its tablespace to a new tablespace 'fast_ssd' so all new dervied tables from this table be in 'fast_ssd' tablespace.
when i try to alter tablespace to 'fast_ssd'.
alter table record_partitioned set tablespace fast_ssd;
i see:
ALTER TABLE
but it seems nothing happened! i check tablespace like this:
SELECT tablespace,tablename FROM pg_tables where tablename='record_partitioned';
and output is:
tablespace | tablename
------------+--------------------
| record_partitioned
tablespace does not change.
There is no way to do this for a partitioned table. You'll have to add an explicit TABLESPACE clause whenever you create a partition.
An alternative is to set the default_tablespace parameter, but that would affect all other tables too.
I just had an RDS instance spun up to a Postgresql db. The database was created successfully and I am able to connect. Problem is when I run this code:
CREATE SCHEMA hollywood;
CREATE TABLE films (title text, release date, awards text[]);
SELECT * FROM hollywood.films;
This is the output I get:
Schema hollywood created
Table films created
An error occurred when executing the SQL command:
SELECT * FROM hollywood.films
ERROR: relation "hollywood.films" does not exist
What am I missing here? I added double-quotes around the schema name but to no avail. I opened up the permissions for the user thusly but to no avail (bad, I know)
grant all privileges on all tables in schema hollywood to bi;
I added the search path before my select statement thusly:
SET search_path TO hollywood; select....
No change.
Try:
CREATE SCHEMA hollywood;
CREATE TABLE hollywood.films (title text, release date, awards text[]);
SELECT * FROM hollywood.films;
or
CREATE SCHEMA hollywood;
SET search_path TO hollywood;
CREATE TABLE films (title text, release date, awards text[]);
SELECT * FROM films;
Using postgres_fdw, I need to create the foreign tables in a specified schema to prevent name collisions. To isolate the issue I've been having, I set up two test postgres databases on the same cluster, import_test and export_test. Export_test has a table, foreign_schema.aa. On the server import_test, after doing the other FDW prerequisites, I ran:
CREATE FOREIGN TABLE local_schema.aa(
id serial NOT NULL,
dat text
) SERVER export_server OPTIONS (table_name 'foreign_schema.aa');
Then:
SELECT * FROM local_schema.aa;
When I do this, I get:
ERROR: relation "local_schema.foreign_schema.aa" does not exist
CONTEXT: Remote SQL command: SELECT id, dat FROM local_schema."foreign_schema.aa"
If I don't do any schema qualification, as in:
CREATE FOREIGN TABLE aa(
id serial NOT NULL,
dat text
) SERVER export_server OPTIONS (table_name 'aa');
And move the aa table to the public schema, the select works just fine.
If the command "SELECT id, dat FROM local_schema."foreign_schema.aa" is literally being run on the remote server, it's obvious why it doesn't work: local_schema."foreign_schema.aa" really doesn't exist on the remote server. For some reason, the postgres_fdw appears to be prepending the name given for table_name with the schema of the foreign table.
I need to specify the schema in the select query, because if I don't it doesn't see the foreign table. Achieving the schema qualification by preceding the select with setting the search path doesn't help either.
Is there anything I am doing wrong? If not, is there a workaround that will let me schema-qualify the foreign table?
EDIT: Per #Craig Ringer's suggestions, here's the self-contained psql input:
CREATE USER test_user SUPERUSER PASSWORD 'password';
SET ROLE test_user;
CREATE DATABASE import;
CREATE DATABASE export;
\c export test_user
CREATE SCHEMA export_schema;
CREATE TABLE export_schema.aa (
id serial PRIMARY KEY,
dat text
);
\c import test_user
CREATE EXTENSION postgres_fdw;
CREATE SERVER export_server
FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (host 'localhost', dbname 'export', port '5432');
CREATE USER MAPPING FOR test_user
SERVER export_server
OPTIONS (user 'test_user', password 'password');
CREATE SCHEMA import_schema;
CREATE FOREIGN TABLE import_schema.aa(
id serial NOT NULL,
dat text
) SERVER export_server OPTIONS (table_name 'export_schema.aa');
SELECT * FROM import_schema.aa;
Which yields this output:
ERROR: relation "import_schema.export_schema.aa" does not exist
CONTEXT: Remote SQL command: SELECT id, dat FROM import_schema."export_schema.aa"
Forgot to come back with resolution. Turns out sometime around the time I posted my bug report, the docs on postgres_fdw were updated. See the section "F.31.1.2. Object Name Options" and the schema_name option on this page: http://www.postgresql.org/docs/current/static/postgres-fdw.html.
I quote the mailing list reply:
"Use: OPTIONS (schema_name 'export_schema', table_name 'aa'); above.
Thanks,
Stephen"
So to resolve, just specify the foreign schema name in the schema_name option parameter.
Can I ALTER an existing table to be UNLOGGED?
PostgreSQL 9.5+ allows setting an existing table as LOGGED / UNLOGGED with the ALTER TABLE command... detailed better here.
For e.g.
ALTER TABLE table_test SET LOGGED;
ALTER TABLE table_test SET UNLOGGED;
The following solution is for PostgreSQL versions<=9.4:
You can do:
create unlogged table your_table_alt as
select * from your_table;
Then:
drop table your_table;
alter table your_table_alt rename to your_table;
I am creating a table with tablespace:
CREATE TABLE SALARY.....
IN ACCOUNTING INDEX IN ACCOUNT_IDX
Where will the Accounting and Account_IDX be created?
The script that you have above will create SALARY in the ACCOUNTING tablespace, with indexes for that table in ACCOUNT_IDX tablespace.
The ACCOUNTING and ACCOUNT_IDX tablespaces need to be created in a separate script that has CREATE TABLESPACE statements.
If you look at the syntax for CREATE TABLESPACE, the USING part of the statement will tell DB2 where to put the files for the tablespace.
DB2 Create Tablespace Reference