alter rename database syntax error inside transaction - postgresql

I have two databases prod_db and prod_db_new and I want to rename both or none. I figured a transaction would suit this case:
BEGIN
ALTER DATABASE prod_db RENAME TO prod_db_old
ALTER DATABASE prod_db_new RENAME TO prod_db
COMMIT;
However, I get syntax error near prod_db_old when run through CLI. What am I missing here ?

You need after all comnands a semicolon
BEGIN;
ALTER DATABASE prod_db RENAME TO prod_db_old;
ALTER DATABASE prod_db_new RENAME TO prod_db;
COMMIT;

Related

How can delete all wrong command in the console?

I have written a wrong create statement in psql console.Create a database mytest and enter it.
postgres=# create database mytest;
CREATE DATABASE
postgres=# \c mytest
You are now connected to database "mytest" as user "postgres".
Type a wrong statement and press enter ,it becomes:
mytest=# create table mytest(report text
mytest(#
Now i write a right statement :
mytest(#create table mytest(report text);
It can't execute ,no new table created ,maybe some cache in the psql ,how to remove them?

postgresql copy from csv file into table in windows through procedure

below is my procedure executed to upload file into table and do joins etc.
CREATE OR REPLACE FUNCTION sp_product_price()
RETURNS void
LANGUAGE 'plpgsql'
COST 100
AS $BODY$
BEGIN
truncate table prd_product_data;
truncate table price_import;
COPY price_import FROM 'C:\Users\Ram\Documents\prices.csv' CSV HEADER;
truncate table active_product_price;
insert into active_product_price(key,name,price)
SELECT prd.key,prd.name,prd.price FROM prd_product_data prd JOIN price_import import ON prd.name = import.name;
raise notice 'success';
END
$BODY$;
Above procedure giving error could not open file "C:\Users\Ram\Documents\prices.csv" for reading: No such file or directory HINT: COPY FROM instructs the PostgreSQL server process to read a file. You may want a client-side facility such as psql's \copy.
I have given access to file for everyone in file properties.
I tried \copy in procedure but this gives error syntax error at or near "\" .
\copy is working when I ran cmd in psql but not in above procedure.
Is there a way to import file into table in above procedure/functions ?
The procedure and the COPY statement are running on the database server, so the file C:\Users\Ram\Documents\prices.csv must be on the database server as well (and your database user must either be a superuser or a member of pg_read_server_files).
The only way you can use COPY to load data from the client is COPY ... FROM STDIN, and you won't be able to use that in a procedure.
\copy is a psql command, not an SQL statement, so it cannot be used in a procedure either.
My suggestion is to use \copy, but to forget about the procedure.

Placeholder in PostgreSQL sql file

I have multiple tables that are created in the same way (same columns, indexes, etc.)
I would like to have one sql file for creating them all without duplicating the create statements.
Is there a way to use some kind of placeholder in sql file which would be substituted when executing the sql file with a parameter?
For example I would like to have below sql statement:
drop table if exists schema.%PLACEHOLDER%;
create table schema.%PLACEHOLDER%(id text, data text);
And execute such script with:
psql -f mysqlfile.sql -magic_parameter my_desired_table_name
Is this possible when executing PostgreSQL sql files, or maybe other way to achieve the same (except using sed)?
Sincr you are using psql, you can use variables as follows:
drop table if exists schema.:placeholder;
The invocation is:
psql -f mysqlfile.sql -v placeholder=table_name

How do you drop a PostgreSQL database with a carriage return in the name?

Somehow, via a restore done through code I ended up with a database with the following name (simulated output from \l command)
Name |
------------------|
\r +|
DATABASE_NAME |
I would like to be able to drop it, but I have no idea how to construct the name properly to include the carriage return when specified via DROPDB or DROP DATABASE commands.
If I can't drop it - can I change the owner to hide it - doesn't that also involve specifying the name?
PostgreSQL 9.2.4 on Ubuntu
Use unicode for line feed while dropping the database like below
drop database U&"\000ADATABASE_NAME";
You can as well ALTER the name like
alter database U&"\000ADATABASE_NAME" rename to "DATABASE_NAME_NEW";
do $$
begin
execute format('drop database %I', E'\rDATABASE_NAME');
end;
$$
Rahul's answer should be
drop database U&"\000ADATABASE_NAME";

Mysql to Posgresql query conversion

Please help to create postgresql query equal to mysql query
LOAD DATA LOCAL INFILE 'file.txt' REPLACE INTO TABLE newtable TERMINATED BY ',' IGNORE 1 LINES;
There is no equivalent feature in PostgreSQL - at least in the current 9.3 or any prior version.
You must do this in a few steps:
CREATE TEMPORARY TABLE ...
COPY into the temp table
Do an UPDATE ... FROM followed by an INSERT INTO ... WHERE NOT EXISTS (...) to merge data
DROP the temp table
Search for "postgresql bulk upsert" or "postgresql copy upsert".
you might be looking for COPY
COPY will be run by the PostgreSQL backend (user "postgres"). The backend user requires permissions to read & write to the data file in order to copy from/to it. You need to use an absolute pathname with COPY. \COPY on the other hand, runs under the current $USER, and with that users environment. And \COPY can handle relative pathnames. The psql \COPY is accordingly much easier to use if it handles what you need.