postgresql error when attempting to do a select all from table - postgresql

I am trying to do the postgresql equivalent of mysql select * from table on a postgresql specific database. I can find the name of the table I need within that database when I do:
SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';
But when I try a select all on the table, I get:
SELECT * from Sample;
SELECT * from Sample;
ERROR: relation "sample" does not exist
LINE 1: SELECT * from Sample;
^
Any ideas?

Postgresql is case sensitive.
I usually use all lower char for field, tables and functions.
Anyway, you can double quote them.
To full answer your question and see why and when useing quote, i suggest to read this specific section:
http://www.postgresql.org/docs/current/interactive/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS

The manes of the tables are stored in information_schema.tables so you have to see your table by using select * from information_schema.tables. IF your table_schema is "public"
try for a table select select * from public.sample, is your table_schema a differed schema chance it to the right one.
this link will help you Psotgresql doc

I tried single-quoting 'Sample' and it didn't work. Fixed by double-quoting "Sample".

Related

append an id onto the name of a table

I have a select statement
select * from schema.table_id
where ID is a number that is passed into a function
how do I append this ID onto the table I have tried || but that gave a syntax error. I have also tried using concat('schema.table_, id::text) but when that ran a select * from %I, table name that gave an error of relation "schema.tablename" does not exist I assume this is due to it being in quotes
This answer is limited to you problem mentioned in the question. You should write it in this way:
format('select * from %I.%I',schema_name,'table_'||id::text)
or
format('select * from "schema_name".%I','table_'||id::text)

Using a synonym in a make-table query ("select into")

I always thought a synonym in T-SQL was just a convenient abbreviation. Yet when I do the following ...
create synonym BACKUP_TABLE for T_SHORT_NAMES_BACKUP
go
select *
into BACKUP_TABLE
from T_SHORT_NAMES
... I get the error that there is already an object called BACKUP_TABLE. Am I doing something wrong?
Synonyms are pointers to other SQL tables. They are extremely useful depending on what you are wanting to do. You can point them to a table in another database, or a table on another server (through a linked server). We leverage them a lot in our ETLs
The process I use to generate mine:
Query to build synonyms dynamically:
SELECT
'CREATE SYNONYM [dbo].[' +TABLE_NAME+ '] FOR [' + 'Put database name here or remove' + '].[dbo].['+TABLE_NAME+']'
FROM
INFORMATION_SCHEMA.TABLES
WHERE
TABLE_TYPE = 'BASE TABLE'
From there, you just SELECT * FROM TABLE_NAME
Now, to circle back to your question. You create a synonym for BACKUP_TABLE that points to T_SHORT_NAMES_BACKUP.
Try: SELECT * FROM BACKUP_TABLE
To find out more about your synonyms: SELECT name, base_object_name FROM sys.synonyms
As select ... into ... always creates a new table object with the given name there must not exist a object with the same name already.
Simply use your select ... into ... standalone, there is no need to add a synonym.
But if you want to add additional rows to your T_SHORT_NAMES_BACKUP use insert into ... select ... instead:
insert into
T_SHORT_NAMES_BACKUP
select
*
from
T_SHORT_NAMES

Possible to use a PostgreSQL TYPE to define a dblink table?

In Postgres, you can link to your other databases using dblink like so:
SELECT *
FROM dblink (
'dbname=name port=1234 host=host user=user password=password',
'select * from table'
) AS users([insert each column name and its type here]);
But this is quite verbose.
I've shortened it up by using dblink_connect and dblink_disconnect to abstract the connection string from my dblink queries. However, that still leaves me with the manual table definition (i.e., [insert each column name and its type here]).
Instead of defining the table manually, is there a way I can define it with a TYPE or anything else that'd be re-usable?
In my case, the number of remote tables I have to join and the number of columns involved makes my query massive.
I tried something along the lines of:
SELECT *
FROM dblink (
'myconn',
'select * from table'
) AS users(postgres_pre_defined_type_here);
But I received the following error:
ERROR: a column definition list is required for functions returning "record"
As you considered creating several types for dblink, you can accept creating several functions as well. The functions will be well defined and very easy to use.
Example:
create or replace function dblink_tables()
returns table (table_schema text, table_name text)
language plpgsql
as $$
begin
return query select * from dblink (
'dbname=test password=mypassword',
'select table_schema, table_name from information_schema.tables')
as tables (table_schema text, table_name text);
end $$;
select table_name
from dblink_tables()
where table_schema = 'public'
order by 1

PostgreSQL query to list all table names?

Is there any query available to list all tables in my Postgres DB.
I tried out one query like:
SELECT table_name FROM information_schema.tables
WHERE table_schema='public'
But this query returns views also.
How can i get only table names only, not views?
What bout this query (based on the description from manual)?
SELECT table_name
FROM information_schema.tables
WHERE table_schema='public'
AND table_type='BASE TABLE';
If you want list of database
SELECT datname FROM pg_database WHERE datistemplate = false;
If you want list of tables from current pg installation of all databases
SELECT table_schema,table_name FROM information_schema.tables
ORDER BY table_schema,table_name;
Open up the postgres terminal with the databse you would like:
psql dbname (run this line in a terminal)
then, run this command in the postgres environment
\d
This will describe all tables by name. Basically a list of tables by name ascending.
Then you can try this to describe a table by fields:
\d tablename.
Hope this helps.
Try this:
SELECT table_name
FROM information_schema.tables
WHERE table_schema='public' AND table_type='BASE TABLE'
this one works!
SELECT table_name
FROM information_schema.tables
WHERE table_type='BASE TABLE'
AND table_schema='public';
For MySQL you would need table_schema='dbName' and for MSSQL remove that condition.
Notice that "only those tables and views are shown that the current user has access to". Also, if you have access to many databases and want to limit the result to a certain database, you can achieve that by adding condition AND table_catalog='yourDatabase' (in PostgreSQL).
If you'd also like to get rid of the header showing row names and footer showing row count, you could either start the psql with command line option -t (short for --tuples-only) or you can toggle the setting in psql's command line by \t (short for \pset tuples_only). This could be useful for example when piping output to another command with \g [ |command ].
How about giving just \dt in psql? See https://www.postgresql.org/docs/current/static/app-psql.html.
select
relname as table
from
pg_stat_user_tables
where schemaname = 'public'
This will not work if track_activities is disabled
select
tablename as table
from
pg_tables
where schemaname = 'public'
Read more about pg_tables

postgres select all tables in the current search path?

I need to find all the visible tables in my current schema search path. I tried:
SELECT *
FROM pg_tables
AND schemaname IN (SHOW search_path)
but it errors with:
PGError: ERROR: syntax error at or near "search_path"
LINE 3: AND schemaname IN (SHOW search_path)
The pg_*_is_visible() set of functions is provided for this purpose. Here is one way to use them:
SELECT * FROM pg_class WHERE relkind IN ('r', 'v') AND pg_table_is_visible(oid);
You can get the current schemas using CURRENT_SCHEMAS() function. It will resolve any references in the search path for you (e.g. $user)
SELECT * FROM pg_tables WHERE schemaname = ANY (CURRENT_SCHEMAS(false));
You could also use the more standard information_schema pseudo-schema.
SELECT * FROM information_schema.tables WHERE table_schema = ANY (CURRENT_SCHEMAS(false));