Table I successfully created missing - postgresql

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;

Related

Copy table between databases

Select * into exp.dbo.client from db.dbo.client
I'm trying to copy a table from one database to another in postgres and it's showing me that the references between the databases aren't implemented.
And if there an easier way to copy the whole database instead of table by table, please tell me.
After executing "create extension dblink" in the database you can after excute this query to copy tableOne from database to tableTwo, without creating it before
create table tableTwo
as
select *
from dblink('host=localhost
user=username
password=password
dbname=current_database',
'select * from tableOne') as linkable( var1 type1, var2 type2,var3 type3,... )
To clone the whole database on the same server:
CREATE DATABASE "db_new" WITH TEMPLATE "db_old" OWNER owner1

Tables are getting created in public schema rather than the other newly created schema in PostgreSQL using PgAdmin4

I am working on PostgreSQL and I want tables in a different schema, not in public.
I am using PgAdmin4. I created one schema and selected the same schema and open the Query Tool. I ran the following query to create the table:
DROP TABLE IF EXISTS ClassToTable;
CREATE TABLE ClassToTable (className varchar(4000), fieldName varchar(4000), tableName varchar(4000) );
But the table is getting created in public schema , rather than the schema which i have created and selected while executing the query.
Can you please help me in resolving this issue or any workaround??

Access table data in local PostgreSQL

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...)

ltree extension installed, but create table still throws error

I added the ltree extension and I can see the extension in my db but when I run the following query:
CREATE TABLE flight_office.document_folder (
document_folder_id serial8,
name varchar,
path ltree,
PRIMARY KEY (document_folder_id)
);
I get this:
ERROR: type "ltree" does not exist
Probably your schema is not included in the search_path.
To install the ltree extension in a schema different from public there are some steps to be done. Let's say your schema is called "myschema":
First create the schema and install the extension:
CREATE SCHEMA myschema;
CREATE EXTENSION IF NOT EXISTS ltree
WITH SCHEMA myschema;
You can check the last step with
SELECT *
FROM information_schema.schemata
WHERE schema_name = 'myschema';
SELECT *
FROM pg_available_extensions
WHERE name = 'ltree';
and
SELECT *
FROM pg_extension;
select *
from pg_catalog.pg_namespace;
where extowner indicates what is the schema where the extension is available.
Then, check whether myschema belongs to the search_path:
SHOW search_path;
; if not add your schema to it:
SET search_path TO "$user", public, myschema;
the latter set the search_path only for the session. If you want to set it permanently use
ALTER DATABASE <DATABASE_NAME> SET search_path TO "$user", public, myschema;
Finally try to create a table:
create table myschema.temptable (
id INTEGER PRIMARY KEY,
path ltree UNIQUE
);
and add some values:
INSERT INTO myschema.temptable (id, path)
values (1, 'root');
As pointed by #pozs, EXTENSION should be added along with the schema,The following worked for me:
CREATE EXTENSION IF NOT EXISTS LTREE
WITH SCHEMA schema_name;
This also works. It will create the extension for the public schema, if no schema is specified.
CREATE EXTENSION IF NOT EXISTS ltree;

How to include schema inf while using dblink in PostgreSQL?

All
I am trying to use dblink in PostgreSQL to run query on different databases. The following works if the table "user" is under the public schema:
select * from dblink(
'hostaddr=1.2.3.4 port=5434 dbname=dbname user=username password=password',
'select id from user')
as t1(
id bigint
);
However, I need to run the query on some other defined schemas. Does anyone know how to add the schema information in the above query? I can't figure it out from the PostgreSQL docs.
When you write SQL query like
SELECT id FROM user
PostgreSQL will resolve table name like user into fully qualified name like schema.tablename using schema search path, which by default is set to "$user",public.
In other words, user will resolve into public.user unless you tweaked server configuration.
However, you can specify schema explicitly in your statement, like this:
SELECT id FROM otherschema.user