NextVal of postgresql usage - postgresql

CREATE SEQUENCE :schema.empseq;
CREATE TABLE emp(empid bigint NOT NULL DEFAULT NEXTVAL(':schema.empseq'));
I am execute like psql -d dbname -U username -f emp.sql -v schema=post
Getting an error
schema ":schema" does not exist

The documentation here talks about how psql interpolates values into SQL.
CREATE SEQUENCE :schema.empseq;
CREATE TABLE emp(empid bigint NOT NULL DEFAULT NEXTVAL(:'schema' || '.empseq'));
might work for you.

Related

Columns combined when using `select ...pgp_sym_decrypt ()` from postgresql databse through Bash psql

System is Debian 11.2 with PostgreSQL 11.5.
I created a database and table as below:
CREATE DATABASE dbname OWNER=postgres
ENCODING= 'UTF8'
\c dbname
CREATE TABLE test(
id serial primary key,
site varchar(100) NOT NULL,
username char(30) NOT NULL,
password char(300) NOT NULL,
note varchar(200) DEFAULT NULL
);
Create bash file as below:
#!/bin/bash
res_user='me'
db_user='postgres'
db_name='dbname'
table_name='test'
sym_key='key'
#insert 4 columns
su $db_user <<EOFU
psql -d "$db_name" -U "$db_user" << EOF
INSERT INTO $table_name (site,username,password,note) VALUES ('v4','u3',pgp_sym_encrypt('password','key','cipher-algo=aes128,compress-algo=0,convert-crlf=1,sess-key=0,s2k-mode=3'),'note3');
EOF
EOFU
#column note has no output
password_arr=($(su $db_user <<EOFU
psql -tAq --field-separator= -d "$db_name" -U "$db_user" << EOF
SELECT "username",pgp_sym_decrypt(password::bytea,'key'),"note" FROM "$table_name" WHERE "site" LIKE '%v4%';
EOF
EOFU
))
echo "${password_arr[1]}" #output is passwordnote3
echo "${password_arr[2]}" #no ouput?
The expect output is:
${password_arr[1]} is `password`
${password_arr[2]} is `note3`
Run above bash script, but output "${password_arr[2]}" has no value,"${password_arr[1]}" is passwordnote3. Where is the problem?
I found the issue. The problem is you specified --field-separator to "nothing" instead of a space. It should be --field-separator=" ". This allowed the output of pgp_sym_decrypt() to concatenate with note. The username field however always had spaces probably since it has a fixed width of 30.
I also suggest that you reduce the number of row outputs to 1, and also enable "noglob" option when you're relying on word splitting. This can be done with set -f. You can also use read to get the needed fields. See How to split a string into an array in Bash?.

Alter postgres table primary key UUID to Character Varying

I want to convert my PostgreSQL table primary key UUID to character varying
ALTER TABLE payment_authorization ALTER COLUMN id TYPE VARCHAR;
When I run the above command showing below error, Beacause foreign key constraints failed. In my system have 200 tables. Is there any easy way to change all tables primary key?
Changing all the tables in place will probably be slow and cumbersome.
The easiest solution might be:
export the database with
pg_dump -F p -f dumpfile.sql dbname
replace uuid with text in the dump using an editor:
sed --in-place -e 's/uuid/text/g' dumpfile.sql
drop and re-create the database:
DROP DATABASE dbname;
CREATE DATABASE dbname;
import the dump:
psql -U postgres -d dbname -1 -f dumpfile.sql

Create tables in a specific schema using psql?

I have a batch that allows me to create a database psql and create tables later.
psql -f "%cd%\db.sql" postgres
echo database creted !
pause
psql -f "%cd%\db_table.sql" mabase postgres
echo tables creted !
the db.sql file:
CREATE DATABASE mabase;
and here is the db_table.sql file:
create table myTable(
idTable INT4 not null,
nom VARCHAR(254) null,
date DATE null,
constraint PK_idTable primary key (idTable)
);
This batch runs as a charme and creates the database as well as the table.
My problem is that the table is created in the public default schema. What I would like to do is create a schema first, that I can do it with one more line in my batch:
psql -f "%cd%\schema.sql" mabase postgres
schema.sql file:
CREATE SCHEMA mabase_schema;
Now the problem is to create the table in the new schema and not in the public schema.
I tried with the line:
psql -f "%cd%\db_table.sql" mabase_schema.mabase postgres
But without success !
How will I be able to define the schema in my batch file?

Postgresql / Timescaledb ERROR: Deprecated trigger function should not be invoked

I work with postgresql boosted with timescaledb fork (pretty impressed with its performance while it worked ;)
I got a script that downloads data, modifies it and puts into a csv file.
Then a psql script is invoked to create a temp table that inserts data into the database
psql -U postgres -d q1 -c "CREATE TABLE tmpp (time bigint NOT NULL, ask real NOT NULL, bid real NOT NULL)"
psql -U postgres -d q1 -c "\copy tmpp (time, ask, bid) from '/sth/sth.csv' delimiter ',' CSV"
psql -U postgres -d q1 -c "insert into realfun select * from tmpp"
psql -U postgres -d q1 -c "DROP TABLE tmpp"
Funny thing is, that it worked for me before, but now I got an error :
ERROR: Deprecated trigger function should not be invoked
I must have messed up sth, but cant figure out what it is [how original]
I will be happy to provide more details, if needed
I cannot find anything similar in google, please advise
It seems that the problem is that you have a newer shared library version than the extension version you have installed (Timescale is an extension, not a fork). You can fix this with ALTER EXTENSION timescaledb UPDATE.
The alter command is documented here.

Dot (.) in Schema name makes pg_dump unusable

I have a schema named 2sample.sc. When I want to pg_dump some of its table, the following error appears:
pg_dump: No matching tables were found
My pg_dump command:
pg_dump -U postgres -t 2sample.sc."error_log" --inserts games > dump.sql
My pg_dump works fine on other schemas like 2sample.
What I did:
I tried to escape dot(.) with no success though
Use "schema.name.with.dots.in.it"."table.name.with.dots.in.it" to specify the schema.table:
-- test schema with a dot in its name
DROP SCHEMA "tmp.tmp" CASCADE;
CREATE SCHEMA "tmp.tmp" ;
SET search_path="tmp.tmp";
CREATE TABLE nononono
( dont SERIAL NOT NULL
);
insert into nononono
SELECT generate_series(1,10)
;
$pg_dump -t \"tmp.tmp\".\"nononono\" --schema-only -U postgres the_database
Output (snipped):
SET search_path = "tmp.tmp", pg_catalog;
SET default_tablespace = '';
SET default_with_oids = false;
--
-- Name: nononono; Type: TABLE; Schema: tmp.tmp; Owner: postgres; Tablespace:
--
CREATE TABLE nononono (
dont integer NOT NULL
);
BTW: why would you want to add a dot to a schema (or table) name? It is asking for trouble. The same for MixedCaseNames. Underscores work just fine.