psql

Display select results vertically in psql, as is done by MySQL's \\G

时光总嘲笑我的痴心妄想 提交于 2019-11-28 17:22:59
In MySQL, you can terminate a select query with \G (as opposed to \g ) to display the results vertically: select * from foo \G *************** id: 1 bar: Hello *************** id: 2 bar: World How can one do the same thing for PostgreSQL using psql? You can do this by enabling Expanded display . Toggle this setting via \x . For example: # \x Expanded display is on. # \x Expanded display is off. When on, results are shown in tabular (vertical) form: -[ RECORD 1 ] id | 1 bar | Hello -[ RECORD 2 ] id | 2 bar | World You can run this for a single command by using the \x\g\x suffix to toggle

Postgresql not creating db with “createdb” as superuser, yet not outputting errors

天涯浪子 提交于 2019-11-28 16:36:51
I am working with a fresh postgresql install, with 'postgres' super user. Logged in via: sudo -u postgres psql postgres=# createdb database postgres-# \list List of databases Name | Owner | Encoding | Collation | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- 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=CTc/postgres template1 | postgres | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 | =c/postgres : postgres=CTc/postgres No errors, yet table is not

Using psql how do I list extensions installed in a database?

ε祈祈猫儿з 提交于 2019-11-28 15:45:06
How do I list all extensions that are already installed in a database or schema from psql? See also Finding a list of available extensions that PostgreSQL ships with In psql that would be \dx See the manual for details: http://www.postgresql.org/docs/current/static/app-psql.html Doing it in plain SQL it would be a select on pg_extension : SELECT * FROM pg_extension http://www.postgresql.org/docs/current/static/catalog-pg-extension.html Additionally if you want to know which extensions are available on your server: SELECT * FROM pg_available_extensions This SQL query gives output similar to \dx

psql - save results of command to a file

不想你离开。 提交于 2019-11-28 15:03:57
I'm using psql's \dt to list all tables in a database and I need to save the results. What is the syntax to export the results of a psql command to a file? From psql's help ( \? ): \o [FILE] send all query results to file or |pipe The sequence of commands will look like this: [wist@scifres ~]$ psql db Welcome to psql 8.3.6, the PostgreSQL interactive terminal db=>\o out.txt db=>\dt db=>\q [wist@scifres ~]$ The psql \o command was already described by jhwist. An alternative approach is using the COPY TO command to write directly to a file on the server. This has the advantage that it's dumped

Psql list all tables

空扰寡人 提交于 2019-11-28 13:25:55
问题 I would like to list all tables in the liferay database in my PostgreSQL install. How do I do that? I would like to execute SELECT * FROM applications; in the liferay database. applications is a table in my liferay db. How is this done? Here's a list of all my databases: postgres=# \list List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- liferay | postgres | UTF8 | en_GB.UTF-8 |

List tables in a PostgreSQL schema

佐手、 提交于 2019-11-28 13:20:25
问题 When I do a \dt in psql I only get a listing of tables in the current schema ( public by default). How can I get a list of all tables in all schemas or a particular schema? 回答1: In all schemas: => \dt *.* In a particular schema: => \dt public.* It is possible to use regular expressions with some restrictions \dt (public|s).(s|t) List of relations Schema | Name | Type | Owner --------+------+-------+------- public | s | table | cpn public | t | table | cpn s | t | table | cpn Advanced users

PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer: “M”

一曲冷凌霜 提交于 2019-11-28 12:47:07
So I have a gender column on my user model and it's currently a string, I'd like to change it to a integer and make Male '1', and Female '0' as it's presently Male "M" Female "F". When running this migration: class ChangeGenderToIntegerOnUser < ActiveRecord::Migration def change change_column :users, :gender, 'integer USING CAST(gender AS integer)' end end I get the following error: error message: PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer: "M" : ALTER TABLE "users" ALTER COLUMN "gender" TYPE integer USING CAST(gender AS integer)/usr/local/rvm/gems/ruby-2.0.0-p247

Importing .csv with timestamp column (dd.mm.yyyy hh.mm.ss) using psql \\copy

泪湿孤枕 提交于 2019-11-28 12:10:48
I'm trying to import data from a .csv file into a postgresql 9.2 database using the psql \COPY command (not the SQL COPY). The input .csv file contains a column with a timestamp in the dd.mm.yyyy hh.mm.ss format. I've set the database datestyle to DMY using. set datestyle 'ISO,DMY' Unfortunately, when I run the \COPY command: \COPY gc_test.trace(numpoint,easting,northing,altitude,numsats,pdop,timestamp_mes,duration,ttype,h_error,v_error) FROM 'C:\data.csv' WITH DELIMITER ';' CSV HEADER ENCODING 'ISO 8859-1' I get this error: ERROR: date/time field value out of range: "16.11.2012 07:10:06" HINT

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

☆樱花仙子☆ 提交于 2019-11-28 09:09:10
问题 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? 回答1: You cannot to use a psql variables inside plpgsql code directly. The symbol substitution is blocked inside

Can't delete database

夙愿已清 提交于 2019-11-28 08:05:13
问题 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 |