psql

Referring to session variables (\\set var='value') from PL/PGSQL

断了今生、忘了曾经 提交于 2019-11-29 15:28:16
I can pass variables into PostgreSQL using psql --variable="var='value'" <<<'SELECT :var' ...and refer to them as, in this case, :var in SQL queries passed to psql on stdin. However, this doesn't work from code using PL/PGSQL: psql --variable=var="'value'" <<'EOF' DO $$ BEGIN SELECT :var; END; $$ EOF ...yielding the error: ERROR: syntax error at or near ":" How can this be resolved? You cannot to use a psql variables inside plpgsql code directly. The symbol substitution is blocked inside strings: postgres=> select :'xx'; ?column? ---------- AHOJ (1 row) postgres=> select ' :xx '; ?column? ----

Can't delete database

老子叫甜甜 提交于 2019-11-29 15:13:39
I'm new to PostgreSQL and am having a problem with what I perceive to be a simple command DROP DATABASE and DROPDB . Why would the following commands not delete my database? postgres=# drop database clientms postgres-# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+--------------+----------+-------------+-------------+----------------------- clientms | clientmsuser | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 | postgres | postgres | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 | template0 | postgres | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 | =c/postgres + | | | | | postgres

Accessing external XML files as variables in a PSQL script (sourced from a bash script)

你说的曾经没有我的故事 提交于 2019-11-29 12:59:32
Following this example , I am having trouble using a PostgreSQL variables in a *.sql script: I want to iterate over a number of XML data files, using a BASH script the BASH script assigns XML file names to a variable, that is passed to SQL script the SQL script, called by that BASH script, loads those data into PostgreSQL If I source the XML files directly, there is no problem; however, I cannot access that variable, in my SQL script: In my SQL script ( hmdb.sql ) I can access the PSQL variable :bash_var (passed from the BASH script): \echo '\nEXTERNAL VARIABLE (= "datafile", HERE):' :bash_var

How to ignore errors with psql \\copy meta-command

放肆的年华 提交于 2019-11-29 07:03:39
I am using psql with a PostgreSQL database and the following copy command: \COPY isa (np1, np2, sentence) FROM 'c:\Downloads\isa.txt' WITH DELIMITER '|' I get: ERROR: extra data after last expected column How can I skip the lines with errors? Erwin Brandstetter You cannot skip the errors without skipping the whole command up to and including Postgres 9.5. There is currently no more sophisticated error handling. \copy is just a wrapper around SQL COPY that channels results through psql. The manual for COPY : COPY stops operation at the first error. This should not lead to problems in the event

Use pg_restore to restore from a newer version of PostgreSQL

落爺英雄遲暮 提交于 2019-11-29 02:02:03
I have a (production) DB server running PostgreSQL v9.0 and a development machine running PostgreSQL v8.4. I would like to take a dump of the production DB and use it on the development machine. I cannot upgrade the postgres on the dev machine. On the production machine, I run: pg_dump -f nvdls.db -F p -U nvdladmin nvdlstats On the development machine, I run: pg_restore -d nvdlstats -U nvdladmin nvdls.db And I got this error: pg_restore: [archiver] unsupported version (1.12) in file header This occurs regardless of whether I choose the custom, tar, or plain_text format when dumping. I found

How to set timezone for Postgres psql?

倖福魔咒の 提交于 2019-11-29 01:51:23
问题 How do I set timezone for psql to something other than my default (US/Central)? Here's what I've tried so far: $ psql psql (9.1.4, server 9.0.4) ... $ psql -c 'show timezone' TimeZone ------------ US/Central $ psql --set=timezone=US/Eastern -c 'show timezone' TimeZone ------------ US/Central $ psql --variable=timezone=US/Eastern -c 'show timezone' TimeZone ------------ US/Central Edit : I don't want to change the server timezone, just the client. Edit #2 : I want it in non-interactive mode.

Shell script to execute pgsql commands in files

旧街凉风 提交于 2019-11-29 01:04:15
I am trying to automate a set of procedures that create TEMPLATE databases. I have a set of files (file1, file2, ... fileN), each of which contains a set of pgsql commands required for creating a TEMPLATE database. The contents of the file (createdbtemplate1.sql) looks roughly like this: CREATE DATABASE mytemplate1 WITH ENCODING 'UTF8'; \c mytemplate1 CREATE TABLE first_table ( --- fields here .. ); -- Add C language extension + functions \i db_funcs.sql I want to be able to write a shell script that will execute the commands in the file, so that I can write a script like this: # run commands

PostgreSQL Nested JSON Querying

浪子不回头ぞ 提交于 2019-11-28 22:25:18
On PostgreSQL 9.3.4, I have a JSON type column called "person" and the data stored in it is in the format {dogs: [{breed: <>, name: <>}, {breed: <>, name: <>}]} . I want to retrieve the breed of dog at index 0. Here are the two queries I ran: Doesn't work db=> select person->'dogs'->>0->'breed' from people where id = 77; ERROR: operator does not exist: text -> unknown LINE 1: select person->'dogs'->>0->'bree... ^ HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. Works select (person->'dogs'->>0)::json->'breed' from es_config_app

Why psql can't connect to server?

可紊 提交于 2019-11-28 18:12:53
I typed psql and I get this: psql: 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"? I used sudo netstat -nlp | grep 5432 to see the status but nothing showed. And I searched online, somebody told me to modify pg_hba.conf but I can't locate this file. And I also tried this command sudo ln -s /tmp/.s.PGSQL.5432 /var/run/postgresql/.s.PGSQL.5432 . It can't work. The error states that the psql utility can't find the socket to connect to your database server. Either you don't

“psql: could not connect to server: Connection refused” Error when connecting to remote database

假如想象 提交于 2019-11-28 17:48:50
I am trying to connect to a postgres database installed in a remote server using the following command: psql -h host_ip -U db_username -d db_name This the error that occurs: psql: could not connect to server: Connection refused Is the server running on host "" and accepting TCP/IP connections on port 5432? Postgres installed version is 9.4. Host operating system : Ubuntu 15.04 Client operating system : Centos 7 I already tried the following but the issue remains unresolved: Edited pg_hba.conf file to include host all all 0.0.0.0/0 md5 Edited 'postgresql.conf' and changed the listen parameter