This question already has answers here:
Postgresql function to create table with dynamic table name?
(1 answer)
PostgreSQL - can't use args in functions
(2 answers)
Closed 5 days ago.
I would like to create users (if not exists) in postgresql server in this way:
$block$
DECLARE
user_name varchar := 'qa_user';
BEGIN
IF NOT EXISTS (
SELECT 1
FROM pg_catalog.pg_roles
WHERE rolname = user_name)
THEN
CREATE USER user_name WITH PASSWORD 'a_random_password';
END IF;
END
$block$
This is an anonymous block because I would like to execute in a more complex database migration task. I have simplified it to better understanding.
The problem this anonymous block, even executed on PgAdmin, is creating the user user_name in database instead of qa_user.
Any idea what I am doing wrong?
Related
This question already has answers here:
Equivalence of from dual in PostgreSQL
(5 answers)
Closed 7 months ago.
I'm trying to convert Oracle to PostgreSQL. While converting I'm getting error like
relation "dual" does not exist .
EXECUTE 'SELECT DBMS_RANDOM.VALUE*(POWER(10,6)) FROM DUAL'
please help me to solve this error.
Dual is a dummy table specific for oracle. In PostgreSQL you can either just use
SELECT random()*power( 10, 6 );
or create your own dual table if you want to keep the dual. (and insert 1 row in it)
You can create a view named dual like:
CREATE VIEW public.dual AS SELECT 'X'::varchar AS dummy;
REF: https://pgpedia.info/d/dual-dummy-table.html
This question already has answers here:
Creating temporary tables in SQL
(2 answers)
Closed 9 months ago.
This script works fine when running on dbeaver, I can work with the new created temp table:
SELECT someField
INTO TEMP tmp_TableZZ
FROM "_fdw_xxx".myTable;
But when inside a stored procedure, I got this error message:
SQL Error [42601]: ERROR: "temp" is not a known variable
Same code:
CREATE OR REPLACE procedure PopulateSomething()
LANGUAGE plpgsql
AS $procedure$
DECLARE v_ReportDte date;
begin
--some code omitted
SELECT someField
INTO TEMP tmp_TableZZ
FROM "_fdw_xxx".myTable;
--some code omitted
end; $procedure$
;
Using TEMPORARY instead of TEMP got the same result.
well, I find out that SELECT INTO in SQL is different from SELECT INTO in pgsql.
The former accepts TEMP as the parameter, the latter expects a variable to store some value.
This question already has answers here:
How to find a table having a specific column in postgresql
(6 answers)
Closed 5 years ago.
I want to search for a particular variable name in ‘PostgreSQL’ database. Similar to the following ‘Teradata’ query
Select TableName, ColumnName from
DBC.Columns
Where ColumnName like (‘%profile%’)
Is there a similar query in PostgreSQL?
Postgres documentation
SELECT table_name,column_name
FROM information_schema.columns
WHERE column_name like '%profile%'
I check below link which I used and running perfectly. But I want to opposite this things.
Postgresql: dblink in Stored Functions
My scenario: Two databases are there. I want to copy one table data from local to remote database. I used dblink for this used but I am confused how to use dblink to store the data?
Local database name: localdatabase
Remote Database name: remotedatabase
Can any one suggest me how can I do this?
Thanks in advance.
Something like the lines below should work:
SELECT dblink_connect('hostaddr=127.0.0.1 port=5432 dbname=mydb user=postgres password=mypasswd');
-- change the connection string to your taste
SELECT dblink_exec('INSERT INTO test (some_text) VALUES (''Text go here'');');
Where test is a table in the remote database with the following definition:
CREATE TABLE test(
id serial
, some_text text
);
After running dblink_exec(), you can check the results in the remote database (or locally, using dblink(), like in the example below).
SELECT * FROM dblink('SELECT id, some_text FROM test') AS d(id integer, some_text text);
id | some_text
----+--------------
1 | Text go here
(1 row)
You can wrap your dblink_exec call in a function as well:
CREATE OR REPLACE FUNCTION f_dblink_test_update(val text, id integer) RETURNS text AS
$body$
SELECT dblink_exec('UPDATE torles.test SET some_text=' || quote_literal($1) || ' WHERE id = ' || $2);
$body$
LANGUAGE sql;
As you can see, you can even build your query string dynamically. (Not that I advocate this approach, since you have to be careful not to introduce a SQL injection vulnerability into your system this way.)
Since dblink_exec returns with a text message about what it did, you have to define your function as RETURNS text unless there are other value-returning statements after the dblink_exec call.
my problem is easy to explain with an example: I have a 'common' schema (the public one?) where I store common data between a clustered application.
For every instance of my application, I have a role (used as the application user).
And i have a common role, app_users, with read-only privileges on the common schema, and every application role is a member of app_users.
Now my problem is: how can i set a trigger on the app_a scheme that execute a function (procedure) in the common scheme, but affect the (and only the) app_a tables?
I mean:
// common_scheme, dummy function to emulate the mysql on update = now()
CREATE OR REPLACEFUNCTION update_etime() RETURNS TRIGGER AS $$
BEGIN
NEW.etime = date_part('epoch'::text, now())::int;
RETURN NEW;
END;
$$ language plpgsql;
// now, in the app_foo scheme, i have the table:
CREATE TABLE foo_table (fid serial not null primary key unique, label char(25));
// and the trigger:
CREATE TRIGGER foo_table_update_etime BEFORE UPDATE ON foo_talbe FOR EACH ROW EXECUTE PROCEDURE update_etime();
// ERROR: function update_etime() does not exist
CREATE TRIGGER foo_table_update_etime BEFORE UPDATE ON foo_talbe FOR EACH ROW EXECUTE PROCEDURE common_scheme.update_etime();
// ERROR: function common_scheme.update_etime() does not exist
The user that will access app_foo has the execute privilege on update_etime() function in common_schema.
Any idea?
I've googled around but the only solution I fount to call functions from other schemas is something like execute 'select * from ' || schema_name || '.table_name'; but i dont think this will do the trick in my case, becose the function must work with the 'local' scheme.
Your second set of syntax should work... the one with "EXECUTE PROCEDURE common_scheme.update_etime();"
If it isn't finding the function, I'd guess that you either have created it in a different schema than you think it is in, or you haven't created it at all (and note, your example create syntax has a bug, no space between "replace" and "function", which would cause an error when trying to create the function. Try doing a:
\df *.update_etime
As superuser to verify the function exists and is in the location you think it is in. HTH.