How can delete all wrong command in the console? - postgresql

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?

Related

alter rename database syntax error inside transaction

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;

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";

Remove external file from postgresql

I added a file to postgres using \i /path/to/some/file.sql but there was a typo in the file.
Here's what I had in the file:
CREATE VIEW holidays AS
SELECT event_id AS holiday_id, title AS name, starts AS date
FROM events
WHERE title LIKE '%Day%' AND venue_id IS NULL;
In the original version I had %DAY%. When running \i /path/to/some/file.sql again, I receive this error:
ERROR: relation "holidays" already exists
How do I undo this relation to get the code to run again?
Thanks
The \i command does not "add a file" that can later be removed. It simply runs the SQL within the file, as if you had typed it at the psql command prompt. If you want to undo what you did, it will depend on what exactly the SQL was.

Running SQL script through psql gives syntax errors that don't occur in PgAdmin

I have the following script to create a table:
-- Create State table.
DROP TABLE IF EXISTS "State" CASCADE;
CREATE TABLE "State" (
StateID SERIAL PRIMARY KEY NOT NULL,
StateName VARCHAR(50)
);
It runs fine in the query tool of PgAdmin. But when I try to run it from the command line using psql:
psql -U postgres -d dbname -f 00101-CreateStateTable.sql
I get a syntax error as shown below.
2: ERROR: syntax error at or near ""
LINE 1:
^
psql:00101-CreateStateTable.sql:6: NOTICE: CREATE TABLE will create implicit sequence "State_stateid_seq" for serial column "State.stateid"
psql:00101-CreateStateTable.sql:6: NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "State_pkey" for table "State"
CREATE TABLE
Why do I get a syntax error using psql and not with pgAdmin?
Run your file 00101-CreateStateTable.sql through a hex dumper. I'll bet you have a UTF-16 marker at the beginning of the file (before the "--" comment characters).
To remove BOM sequence on Ubuntu you can use bomstrip, bomstrip-files
What version(-s) do you use? IF EXISTS came with version 8.2, maybe you're connection with version 8.1 or older when you use psql.
Thank you guys. I have been struggling with this issue for a few weeks. I could not run my SQL scripts using PSQL. I thought I have some issues with my OS, now I know it is the BOM issue in my text file. I installed bomstrip packet in Ubuntu and now all my SQL scripts are working again.