psql

psql: Init files?

。_饼干妹妹 提交于 2019-12-11 03:16:51
问题 In the psql documentation, I read information about variables (section advanced features), e.g. one of these variables is: HISTSIZE The number of commands to store in the command history. The default value is 500. Is there a file in the home directory or somewhere else where I can configure these variables? What syntax would I use in that file? 回答1: If you look at the Files section, you'll see this: Files Unless it is passed an -X or -c option, psql attempts to read and execute commands from

string substitution with query result postgresql

一世执手 提交于 2019-12-11 02:38:06
问题 I have a query which will always return only one element as a result. I want to append the result of this query into a string which I can use for futher processing Example select id from ids where some_condition I want to append this id in a string like result_(id)_table , where, the id has to be substituted with the id returned from the previous query (Which is essentialy an another table) and finally, I should be able to execute a query like select * from result_id_table Where, "result_id

psql return value / error killing the shell script that called it?

自作多情 提交于 2019-12-11 02:34:35
问题 I'm running several psql commands inside a bash shell script. One of the commands imports a csv file to a table. The problem is, the CSV file is occasionally corrupt, it has invalid characters at the end and the import fails. When that happens, and I have the ON_ERROR_STOP=on flag set, my entire shell script stops at that point as well. Here's the relevant bits of my bash script: $(psql \ -X \ $POSTGRES_CONNECTION_STRING \ -w \ -b \ -L ./output.txt -A \ -q \ --set ON_ERROR_STOP=on \ -t \ -c "

PostgreSQL: .psql_history to /dev/null

99封情书 提交于 2019-12-11 00:27:00
问题 Rather than apply a \set HISTFILE command for every possible connection, we would like to have no .psql_history at all. However, psql seems to dislike that: % ln -s /dev/null ~/.psql_history % psql psql (8.4.8) postgres=# select version(); ... postgres=# \q could not save history to file "~/.psql_history": Not owner There are folks out there willing to patch the source to get a fix but we prefer to be on a vanilla binary installation whenever possible: to us, this isn't worth the hack. To

Unable to restore pg_dump backup

雨燕双飞 提交于 2019-12-10 16:12:36
问题 I'm trying to restore backup from Postgres 9.1.1, which was created by: pg_dump mydb > backup.sql restore on Postgres 9.1.9 psql -d mydb -f backup.sql I'm getting this error: psql:datasets.sql:278537: invalid command \. psql:datasets.sql:278544: ERROR: syntax error at or near "1" LINE 1: 1 4446 49 253.412262 239.618317 0 211.54303 100.482948 197.1... The \. is part of COPY command, I guess it should be compatible between Postgres 9 versions, or it's not? COPY data_136 (id, in_1, in_2, in_3,

Wild Card Before and After a String - MySql, PSQL

南笙酒味 提交于 2019-12-10 15:56:00
问题 I need to perform Contains operation in a column. For Contains operation we need use Wildcard before and after a word. Ex: personalized Query -> like '%sonal%' As this type of query can't use indexes. Is there any way to increase the speed of the search. Note: I use MySql(InnoDB) and PSQL 回答1: PostgreSQL has solution - trigram index. Here is a article or documentation postgres=# create extension pg_trgm ; CREATE EXTENSION postgres=# create index on obce using gin (nazev gin_trgm_ops); CREATE

psql - how to flush database content without dropping table

纵然是瞬间 提交于 2019-12-10 13:46:37
问题 I have a table in my db called 'mytable'. I'd like to clear it so that I can continue to collect and analyze 'fresh data' from it. Something like conn = psycopg2.connect(database = mydb_name, host = mydb_server, user = mydb_uname, password = mydb_pwd) cur = conn.cursor() cur.execute("DROP TABLE mytable;") Isn't going to work for me, because as far as I understand it, this destroys the table. I don't want to destroy/re-create... Just to flush all data. How can I work this out? 回答1: Truncate

How to download bytea column as file using Java

≡放荡痞女 提交于 2019-12-10 11:46:25
问题 I want to download files stored in bytea format using java. I don't have superuser privileges. Using the code below I download the hex encoded file and convert it to pdf but the converted pdf is damaged whereas if I copy using \copy function(cannot use in java) via terminal, downloading process works smoothly. String sql = "(SELECT encode(f,'hex') FROM test_pdf where id='2' LIMIT 1)"; System.out.println(sql); CopyManager copyManager = new CopyManager((BaseConnection) conn); FileWriter filew =

Get all tags by context for acts-as-taggable-on

心不动则不痛 提交于 2019-12-10 11:29:21
问题 We use https://github.com/mbleigh/acts-as-taggable-on for our Rails app and we've encountered a problem. We tell users the possible tags for a given model and context based on the recommended (Acts-as-taggable-on find all tags by context) Model.tag_context_counts , but this is generating horribly inefficient queries. As our site is growing this is becoming a problem. For some reason it generates this kind of thing: SELECT recipes.id FROM "recipes" ORDER BY sort_order SELECT tags.*, taggings

How to generate a CREATE script for several tables in pgAdmin III?

好久不见. 提交于 2019-12-09 13:42:06
问题 In pgAdmin III you can: right-click a table; scripts; CREATE script; save the script from the SQL Editor. If one has to do this for more than one table, is there a way to combine the scripts in one file (apart from manually copy-pasting them)? If this can be done via psql prompt or phppgadmin, that will be ok too. 回答1: Here's a way using pgAdmin. Right-click on your database (or schema). Choose "backup" Under "Format" choose "plain" Under "Dump Options #1" choose "Only schema" Under "Objects"