psql

Truncating display by default in postgres psql select statements

一曲冷凌霜 提交于 2019-12-06 05:27:28
问题 I have a table with a long text column. I'd like to be able to select all of the columns but limit the text column without needing to write every column. select * from resources; Produces an output that is too long to display correctly in psql. I can get something to show up by using substr() or left() on the long column, but then I need to specify each column. select id, left(data, 50), file_format_version, ... from resources; Is there a way that I can just get psql to truncate long columns

Get mysqldump to dump data suitable for psql input (escaped single quotes)

陌路散爱 提交于 2019-12-06 04:36:16
问题 I'm trying to port a database from MySQL to PostgreSQL. I've rebuilt the schema in Postgres, so all I need to do is get the data across, without recreating the tables. I could do this with code that iterates all the records and inserts them one at a time, but I tried that and it's waaayyyy to slow for our database size, so I'm trying to use mysqldump and a pipe into psql instead (once per table, which I may parallelize once I get it working). I've had to jump through various hoops to get this

Docker compose postgresql service - can't create user and database during build?

心不动则不痛 提交于 2019-12-06 00:34:13
问题 I have wasted an entire day on this, and to say I'm not impressed by the unnecessary complexity of what should be a simple task - would be a gross understatement. Ok, having got that off my chest, I am building a django application using docker-machine, docker-compose, postgresql and redis - by following this tutorial. I have managed to get the basic tutorial to work - but it does not suit my needs, as I need to create a user and a database for my application - as opposed to using 'postgres'

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

安稳与你 提交于 2019-12-05 20:22:08
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 currval('id_data_id_seq')) scalasb=> \echo :the_id 54754 scalasb=> But this is what I get: scalasb=>

how to use \\timing in postgres

柔情痞子 提交于 2019-12-05 17:59:11
I want to know the time that it takes to execute a query in Postgres, I see a lot of response that propose to use \timing, but I'm newbie in Postgres and I don't know how to use it, can anyone help thank you in advance You can use \timing only with the command line client psql , since this is a psql command. It is a switch that turns execution time reporting on and off: test=> \timing Timing is on. test=> SELECT 42; ┌──────────┐ │ ?column? │ ├──────────┤ │ 42 │ └──────────┘ (1 row) Time: 0.745 ms test=> \timing Timing is off. 来源: https://stackoverflow.com/questions/40593723/how-to-use-timing

psql - write a query and the query's output to a file

霸气de小男生 提交于 2019-12-05 17:34:13
问题 In postgresql 9.3.1, when interactively developing a query using the psql command, the end result is sometimes to write the query results to a file: boron.production=> \o /tmp/output boron.production=> select 1; boron.production=> \o boron.production=> \q $ cat /tmp/output ?column? ---------- 1 (1 row) This works fine. But how can I get the query itself to be written to the file along with the query results? I've tried giving psql the --echo-queries switch: -e, --echo-queries Copy all SQL

How to get definition/source code of an aggregate in PostgreSQL?

随声附和 提交于 2019-12-05 13:10:31
I found this related answer useful: Export "Create Aggregate" functions from PostgreSQL But how do I get the CREATE AGGREGATE statement without a GUI client (e.g. with psql command line)? a_horse_with_no_name Something like this, but I'm not sure if this covers all possible ways of creating an aggregate (it definitely does not take the need for quoted identifiers into account) SELECT 'create aggregate '||n.nspname||'.'||p.proname||'('||format_type(a.aggtranstype, null)||') (sfunc = '||a.aggtransfn ||', stype = '||format_type(a.aggtranstype, null) ||case when op.oprname is null then '' else ',

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

拥有回忆 提交于 2019-12-05 05:56:58
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... 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 qwe from data ORDER BY qwe; You could just use double quotes ( " ) for the shell quoting and single quotes (

How to save Amazon Redshift output to local CSV through PSQL on SQL WorkBench?

纵然是瞬间 提交于 2019-12-05 05:40:06
I am writing psql through Amazon Redshift and now I am trying to save the output as CSV through PSQL query, on SQL WorkBench The reason I am planning to do this through query instead of using select clause and then right click to save the output as csv, is because there are large amount of data, I found that if I generate the output into a temp table, it's much much faster than using select to display all the output. Therefore, I am thinking whether saving to local CSV can be faster too. I have tried the top solution here , however, it doesn't work on Amazon Redshift, When I am using Copy

Difference between set, \\set and \\pset in psql

纵饮孤独 提交于 2019-12-05 01:29:42
I get a little confused some times when working with psql between when to use a set vs. \set vs. \pset . I think that: set is for session variables on my connection to the db. For example SET ROLE dba ; \set is for local variables for this psql session. For example \set time 'select current_timestamp' \pset is for psql settings for this psql session. For example '\pset border 2' But, I've never found what I thought was a good explanation of each. Are my assumptions above correct? I'm using PostgreSQL 9.4 Basically correct. The important difference is that SET is an SQL command while the other