psql

Using positional parameter ($1,..) in psql

给你一囗甜甜゛ 提交于 2019-12-23 09:04:06
问题 I often want to copy/paste sql out of my program code and test/debug in psql and it is tedious to have to replace positional arguments with literal values. Is there a good way to convert: select * from users where name=$1 and email=$2; to: select * from users where name='troy' and email='t@me.com'; 回答1: You could use psql variables. Those are interpolated in SQL code. Per documentation: A key feature of psql variables is that you can substitute ("interpolate") them into regular SQL statements

Duplicate key value violates unique constraint “A_users_pkey” Detail: Key (A_name)=(1) already exists

南楼画角 提交于 2019-12-23 04:52:52
问题 I'm trying to insert a string Value with comma separators and it works fine INSERT INTO users VALUES (133141214,regexp_split_to_table('rock,jackel', ',')); but my table has pkey Constraint to user name.When I Try to insert another String Value as follow INSERT INTO users VALUES (144141214,regexp_split_to_table('rock,raffel', ',')); My query has new value raffel in it.that raffel should enter into table but because of pkey the query is failing and saying Duplicate key value violates unique

Where does the file output of pg_dump go on MacOS?

馋奶兔 提交于 2019-12-23 03:23:27
问题 In psql , I just ran the command pg_dump to a custom-format archive: Kurts-MacBook-Pro-2:~ kurtpeek$ psql --host localhost.aptible.in --port 64186 --username aptible --dbname db Password for user aptible: psql (10.4, server 10.3 (Debian 10.3-1.pgdg80+1)) SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off) Type "help" for help. db=# pg_dump --format=custom db-# As I understand it, this should back up the database (of which the name is obtained

Create SQL function referring to a table or column that does not exist (yet)

故事扮演 提交于 2019-12-22 18:33:39
问题 I want to load some SQL functions in a empty database through psql: psql -d my_database -f fuctions.sql --set ON_ERROR_STOP=1 I use --set ON_ERROR_STOP=1 because I want that psql fails if the script contains errors. The content of functions.sql is: CREATE or REPLACE FUNCTION test_function() RETURNS INT AS $$ SELECT id from test_table; $$ LANGUAGE sql; My problem is, that psql checks if test_table exists when loading function and fails with this error: ERROR: relation "test_table" does not

How to create multiple tables using for loop in postgresql

孤者浪人 提交于 2019-12-22 18:24:49
问题 How does one create multiple tables using a for loop in postgresql? For example : I need to create c_emloyee, r_employee, i_employee, etc. I got a syntax error near FOR ! /bin/sh #Invoke postgre SQLVARCHAR="varchar" SQLINTEGER="integer" SQLBIGINT="bigint" SQLSMALLINT="smallint" SQLTINYINT="smallint" SQLCIDR="cidr" SQLBINARY="varbinary" SQLTIME="timestamp" SQLMACADDRESS="macaddr" prefix[0]=c_ prefix[1]=r_ prefix[2]=s_ prefix[3]=i_ echo ${prefix[0]} echo ${prefix[1]} echo ${prefix[2]} echo $

PostgreSQL encoding issue while executing query from command line

我与影子孤独终老i 提交于 2019-12-22 15:04:59
问题 I am trying to execute an SQL query which is stored in the file. I am using following command to execute: psql -d DB_NAME -a -f QUERY_NAME.sql I have some non English text in the SQL file like - સુરત When the query is executed the text in the database looks like - à ª¸à «Âà ª°à ª¤ How do I execute the query from command line so that it runs correctly? 回答1: Make sure the client_encoding matches the encoding of your file. Check your system locale. Then use a matching command line

PSQL: How can I prevent any output on the command line?

雨燕双飞 提交于 2019-12-22 11:38:30
问题 My problem: I'm trying to run a database generation script at the command line via a batch file as part of a TFS build process to enable nightly testing on a known dataset. The scripts we run are outputting Notices, Warnings and some Errors on the command line. I would like to suppress at least the Notices and Warnings, and if possible the Errors as they don't seem to have an impact on the overall success of the scripts. This output seems to be affecting the success or failure of the process

How do I escape single quote in command line query of psql?

本小妞迷上赌 提交于 2019-12-22 05:08:29
问题 I googled a lot but.. How do I escape single quote in command line query of psql ? psql -t -A -F $'\t' postgresql://zzzz:5432/casedb -U qqqq -c 'select id,ext_ids ->> 'qwe' as qwe from data ORDER BY qwe' > /jdata/qwe.tab Results in error ERROR: column "qwe" does not exist LINE 1: select id,ext_ids ->> qwe as qwe from data... 回答1: In Postgres you can use dollar-quoted strings: select id,ext_ids ->> $$qwe$$ as qwe from data ORDER BY qwe; -- or select id,ext_ids ->> $anything$qwe$anything$ as

How to start psql.exe

青春壹個敷衍的年華 提交于 2019-12-22 04:19:32
问题 I'm new to postgresql (pg). I understand that in order to interact with pg, I must use psql.exe. In my system, I find psql.exe not once, but twice, why? ( C:\Program Files (x86)\pgAdmin 4\v2\runtime\psql.exe and C:\Program Files\PostgreSQL\10\bin\psql.exe ). I tried both, with identical (negative) results. In line with Q/A In PostgreSQL why does command line window disappear when I press Enter after entering my password?, I now, instead of running psql.exe directly, I first opened a generic

how to use variables in a psql script

感情迁移 提交于 2019-12-21 20:23:30
问题 Here is my psql script, which does not work: \set path '''c:\\server\\data\\'''; COPY paymentMethods (name,regexString) FROM :path+'paymentMethods.csv' WITH (FORMAT csv, HEADER true); COPY priceLevels (name) FROM :path+'priceLevels.csv' WITH (FORMAT csv, HEADER false); psql complains about the syntax error at the + How can I change it so that it works, while having the actual path string mentioned just once? 回答1: First of all, you are trying to concatenate two strings with + operator, but the