psql

How to download bytea column as file using Java

♀尐吖头ヾ 提交于 2019-12-08 12:53:41
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 = new FileWriter("/home/sourabh/image.hex"); copyManager.copyOut("COPY "+sql+" TO STDOUT ", filew );`

using copy in postgresql?

Deadly 提交于 2019-12-08 11:56:08
问题 There is a table with 3 columns mydocs(id serial, docform integer, content text) copy (select (content) from mydocs where id=30 ) to 'D:/html/ex10.xml' ; I choose 1 row ( id=30 ) with this expression and put (content text) from it in folder with path. It works but <?xml version="1.0" encoding="utf-8"?> \r\n <tutorial> \r\n <title>"Заметки об XSL"</title> \r\n <author>лермонтов</author> \r\n </tutorial> In folder doc have additional symbols like \r and \n, how to delete them when i copy it, or

Postgres JSONB: query values in JSON array

本小妞迷上赌 提交于 2019-12-08 06:35:44
问题 Postgres 9.4 I have a record with a JSONB value like this: { "attributeA": 1, "attributeB": "Foo", "arrayAttribute": [ {"attributeC": 95, "attributeD": 5}, {"attributeC": 105, "attributeD": 5} ] } I want to write a query which says: Find any item where attributeA = 1, attributeB = 'Foo', and for each element in the arrayAttribute array, attributeC is within a 10 point range of some value X. So if X was 100, the above record would match (both 95 and 105 are within 10 points from 100). I'm

Redshift - Convert epoch string to timestamp

半城伤御伤魂 提交于 2019-12-08 05:36:07
问题 I have got a table with 2 columns epoch_start and epoch_end. I want to find the difference in days of these 2 epochs. The problem i am facing is that the above columns are character varying(5000) type. The query im running is select datediff(day,'1459762341','1450762341') as numdays; The error i get is ERROR: invalid input syntax for type timestamp: "1459762341" 回答1: I have found the solution - To get timestamp from epoch - SELECT (TIMESTAMP 'epoch' + '1459762341' * INTERVAL '1 Second ') as

gzip table to csv on AWS instance, prior to downloading / saving locally with psql

有些话、适合烂在心里 提交于 2019-12-08 05:28:30
问题 I'm new to PostgreSQL and the psql CLI. My bandwidth is extremely limited, which results in it taking hours to download each table from an AWS instance, that are 1 - 5 GB's each. The current command I use, after logging into the DB with psql: \copy (SELECT * FROM table) TO table.csv CSV DELIMITER ',' Is it possible to query a table, similar to the above, that actually zips the csv file ON the Amazon PostgreSQL instance, prior to downloading and saving locally, thus reducing the 1 - 5 GB

unable to create postgis extension ERROR: could not access file “$libdir/postgis-2.0”: No such file or directory

微笑、不失礼 提交于 2019-12-08 04:26:42
问题 When I try run this command sudo -u postgres psql -h localhost -d template_postgis -f /Users/MyUser/postgis-2.0.6/postgis/postgis.sql I get this error SET BEGIN psql:/Users/MyUser/postgis-2.0.6/postgis/postgis.sql:49: ERROR: could not access file "$libdir/postgis-2.0": No such file or directory psql:/Users/MyUser/postgis-2.0.6/postgis/postgis.sql:54: ERROR: current transaction is aborted, commands ignored until end of transaction block . . . psql:/Users/MyUser/postgis-2.0.6/postgis/postgis

pg_dump & pg_restore password using python module subprocess

你离开我真会死。 提交于 2019-12-07 20:30:10
问题 Problem: Use the PSQL pg_dump and pg_restore in a Python script and using the subprocess module. Background: I am using the following python 2.7 script from the localhost (i.e. Ubuntu 14.04.5 LTS ) to create a backup of a table in a PSQL server (i.e. PostgreSQL 9.4.11 ) and restore it into the remote host (i.e. Ubuntu 16.04.2 LTS ) in a newer version of PSQL server (i.e. PostgreSQL 9.6.2 ). #!/usr/bin/python from subprocess import PIPE,Popen def dump_table(host_name,database_name,user_name

gzip table to csv on AWS instance, prior to downloading / saving locally with psql

喜你入骨 提交于 2019-12-07 17:15:27
I'm new to PostgreSQL and the psql CLI. My bandwidth is extremely limited, which results in it taking hours to download each table from an AWS instance, that are 1 - 5 GB's each. The current command I use, after logging into the DB with psql: \copy (SELECT * FROM table) TO table.csv CSV DELIMITER ',' Is it possible to query a table, similar to the above, that actually zips the csv file ON the Amazon PostgreSQL instance, prior to downloading and saving locally, thus reducing the 1 - 5 GB downloads to < 1 GB; significantly reducing the download times? Something like: \copy (SELECT * FROM table)

How to bind a SQL query return value to a psql variable?

半世苍凉 提交于 2019-12-07 17:05:51
问题 Background: I'm writing my first pgTAP test cases for a PL/pgSQL function and starting small with psql test scripts. No problems on that but I run into a small annoyance on psql variables. In my test scripts I first dump quite a bit of test data to a relevant tables and later refer to the data with a primary keys that were generated by sequences. I'd find it handy to be able to create a variable that would contain the primary key. This is what I'm looking for: scalasb=> \set the_id (select

PostgreSQL连接和认证选项

大城市里の小女人 提交于 2019-12-07 16:40:44
Linux下面经常用到psql命令。 切换Linux系统账号到root账号。 #CentOS Debian su root #Ubuntu sudo -i #切换到postgres账号 su - postgres #尝试不加-区别 su postgres 该账号下即可输入psql命令。演示代码如下 root@debian:~# su - postgres postgres@debian:~$ postgres@debian:~$ psql psql (9.6.5) 输入 "help" 来获取帮助信息. postgres=# create database test; CREATE DATABASE postgres=# \c test 您现在已经连接到数据库 "test",用户 "postgres". test=# #退出psql命令 test=# \q postgres@debian:~$ psql帮助信息如下 postgres@debian:~$ psql --help psql是PostgreSQL 的交互式客户端工具。 使用方法: psql [选项]... [数据库名称 [用户名称]] 通用选项: -c,--command=命令 执行单一命令(SQL或内部指令)然后结束 -d, --dbname=数据库名称 指定要连接的数据库 (默认:"postgres") -f, -