psql

how to use \timing in postgres

不羁岁月 提交于 2019-12-07 11:41:46
问题 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 回答1: 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:

Querying data within JSON array data field

可紊 提交于 2019-12-07 02:15:28
问题 Currently playing with JSON fields in postgres 9.3 for the first time and Im running into some difficulty with querying arrays. The field with the JSON array data type is called 'accounts' and some sample data would be as follows [{name: "foo", account_id: "123"}, {name: "bar", account_id: "321"}] I want to be able to find the id of the company that owns account_id 123 for example. The query that I'm having trouble with currently is as follows: select id from companies where json_array

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

拜拜、爱过 提交于 2019-12-06 22:38:46
问题 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

How do I declare variables in pgAdmin

跟風遠走 提交于 2019-12-06 22:22:16
问题 If I am in the psql terminal then I can declare and use a variable like this: \set message_id soifsdaofisd.gmail.com; select * from emails where message_id = ':message_id'; How can I do this in pgAdmin? I get an error when ever I try this in pgAdmin: ERROR: syntax error at or near "CALAdkA4YC0" LINE 3: set message_id soifsdaofisd.gmail.com. 回答1: \set is a feature of psql (the interactive command line terminal) and not available in pgAdmin. PostgreSQL does not normally use variables in plain

Difference between set, \set and \pset in psql

人盡茶涼 提交于 2019-12-06 19:39:03
问题 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

will pg_restore overwrite the existing tables?

↘锁芯ラ 提交于 2019-12-06 19:01:11
问题 Say I have two host servers s1 and s2. In both the servers i have a schema named n1, now i have made some changes to some of the tables present in schema n1 of s1. I want the same change to be done to schema n1 of server s2. what i am planning to do is to take a backup of the schema n1 of server s1 using pg_dump and restore in the server s2 using pg_restore. Now my question is ,since there is already the same schema n1 in the server s2 with the same set of tables. what the restore process

PostgreSQL encoding issue while executing query from command line

∥☆過路亽.° 提交于 2019-12-06 13:38:50
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? Make sure the client_encoding matches the encoding of your file. Check your system locale. Then use a matching command line argument for psql. Quoting the manual here: If at least one of standard input or standard output are a terminal,

pg_dump & pg_restore password using python module subprocess

元气小坏坏 提交于 2019-12-06 10:00:19
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,database_password,table_name): command = 'pg_dump -h {0} -d {1} -U {2} -p 5432 -t public.{3} -Fc -f /tmp

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

不问归期 提交于 2019-12-06 08:46:57
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 as far as the TFS build process is concerned. It's highlighting every line of output from the scripts

psql passed variable

断了今生、忘了曾经 提交于 2019-12-06 06:58:07
New to psql scripting. I try to pass a variable to a psql script but get an error: psql -v dateav="2012-01-28" mcdb -p 5555 -U admin -q -t -A -c 'select count (client_name) from v_activities where scheduled_start_date like :'dateav';' ERROR: syntax error at or near ":" LINE 1: ...) from v_activities where scheduled_start_date like :dateav; Any ideas? Would work like this: echo "select count (client_name) from v_activities \ where scheduled_start_date like :'dateav'" | \ psql -v dateav="2012-01-28" mcdb -p 5555 -U admin -q -t -A Explain: I quote the manual here : -c command (...) command must