psql

PostgreSQL drop constraint with unknown name

…衆ロ難τιáo~ 提交于 2020-01-11 05:11:45
问题 I have an SQL script that needs to drop several constraints and restore them at the end, but the constraint names are auto-generated and will be different each time the script is run. I know how to get the constraint name from the table names, but it doesn't seem possible to use this information in the drop statement. select conname from pg_constraint where conrelid = (select oid from pg_class where relname='table name') and confrelid = (select oid from pg_class where relname='reference table

Use variable set by psql meta-command inside of DO block

£可爱£侵袭症+ 提交于 2020-01-10 05:11:21
问题 Here's what I would like to do: \set values foo,bar,baz DO $$ DECLARE value TEXT; values TEXT[] := string_to_array(:'values', ','); BEGIN FOREACH value IN ARRAY values LOOP raise notice 'v: %', value; END LOOP; END $$ LANGUAGE plpgsql; Which results in the following error: ERROR: syntax error at or near ":" SELECT string_to_array(:'values', ',') INTO values... ^ Here's the solution I have currently, but it feels hacky: \set values foo,bar,baz PREPARE get_values AS SELECT string_to_array(:

how to specify format of timestamp while creating table in postgreSQL?

…衆ロ難τιáo~ 提交于 2020-01-06 14:14:26
问题 I have created table with query: CREATE TABLE Z ( A varchar(30), B varchar(30), C timestamp, D numeric, E varchar(30), F varchar(30), G varchar(30), H numeric ); Now, I have a csv file with delimiter '|' and entries in this file are like 1313131|131313131|20130103115041|20.0000000|ABCD|EFGH||0.0000000 I am trying following command to import the data in the table: \copy Z from filename WITH (FORMAT CSV , DELIMITER '|', NULL ''); But I am getting following error: ERROR: invalid input syntax for

postgres no users or roles, can't create but works with rails?

僤鯓⒐⒋嵵緔 提交于 2020-01-05 08:55:22
问题 I'm trying to setup postgres on OSX. I was having problems with a rails app ('cannot connect to database') and reinstalled postgres with homebrew. I cannot issue postgres commands from terminal now though: $ sudo -u _postgres psql Password: psql: FATAL: role "_postgres" does not exist $ brew list autoconf libksba openssl python sqlite automake libtool ossp-uuid rbenv gdbm libyaml pkg-config readline libgpg-error mongodb postgresql ruby-build $ postgresql zsh: command not found: postgresql $

only set variable in psql-script if not specified on the command-line

江枫思渺然 提交于 2020-01-05 03:58:07
问题 I want to give the the option to specify some variables on the command-line for my psql-scripts with. psql -v myVar=myValue However I found no way to give those variables default values within the sql-script itself. The syntax: `\set MyVar defaultValue` overwrites the value myValue specified on the psql command-line unconditionally. Is there any way to check if a variable is set within psql? 回答1: Setting default values for variables is not supported in psql, but you can do it using a

PostgreSQL error: could not connect to database template1: could not connect to server: No such file or directory

僤鯓⒐⒋嵵緔 提交于 2020-01-03 06:53:40
问题 I need to create database. First I run: sudo su - postgres , then: createdb test And I keep getting this error: createdb: could not connect to database template1: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432" This is unexpected, I have never encountered any problems with psql, but this time I don't get what is wrong. And there is no external server, I am connected locally

How can I update a single field in psql with the contents of a file?

拥有回忆 提交于 2020-01-03 05:43:07
问题 I am trying to do the following: UPDATE bodycontent set body=CONTENT_FROM_FILE where contentid=12943363; I tried the following based on the highest voted answer to this question. \set contentfill `cat Foo.txt` UPDATE bodycontent set body=:'contentfill' where contentid=12943363; This results in the following error. ERROR: syntax error at or near ":" LINE 1: UPDATE bodycontent set body=:'contentfill' where contentid=1... Is there a clean, simple and effective way to achieve this on the psql

psql passed variable

故事扮演 提交于 2020-01-02 12:39:29
问题 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? 回答1: Would work like this: echo "select count (client_name) from v_activities \ where scheduled_start_date like :'dateav'" | \ psql -v dateav=

Display all database names

有些话、适合烂在心里 提交于 2019-12-31 07:01:30
问题 is there any way to display all database names using the host address and port? Like SELECT current_database() which shows currently connected db. I need to display all database names. Thanks in advance. 回答1: There is a table which shows all databases: SELECT * FROM pg_database; 来源: https://stackoverflow.com/questions/48656360/display-all-database-names

postgresql extracting function source

不羁的心 提交于 2019-12-31 04:48:08
问题 Need to extract the source of a PostgreSQL function using SQL. I am seeing this odd behavior with one of the function, all other functions (around 200+ ) work absolutely fine. When I run the following statement, it works: select prosrc from pg_proc where proname= 'accounts_count'; However when I run the following, it returns an empty string: select routine_definition from information_schema.routines where specific_name = 'accounts_count_66243' PostgreSQL version 8.3. I have tried using both