Syntax Error When Using pgbench - postgresql

I am attempting to obtain some benchmarking data by using pgbench, but I am running into a syntax error in the process. I am running psql v9.6.2 on macOS High Sierra (10.13.2), and consistently receive the same error when using pgbench in the psql command-line client, and in PGAdmin IV:
syntax error at or near "pgbench"
An example command I might run would be:
postgres=# pgbench -i -s 2000 [db_name];
I'm sure there is something simple that I'm missing here, but the documentation page on pgbench hasn't been too illuminating for me.

pgbench is a separate command line program shipped with postgres
run it on console, not like a script

Related

Postgres import from pg_dump throws error of none existing character: syntax error at or near "ÿ_"

My first post here may go a bit against the grain - if it does violate guidelines feel free to delete. The problem itself has been solved, but since it was quite frustrating and the solution is documented nowhere, however simple it is, I thought I post here for fellow devs encountering the same. Beyond that, if someone can shed some light as to why this happened in the first place, Answers are more than welcome.
The Issue
Downloading data from local PostgreSQL instance using pg_dump from a DB1
Full command: pg_dump -U {user} -d {sourceDB} -t {sourceTable} > {dump}.sql
Uploading the data on the same server in a different DB2 using psql
Full command: psql -U {user} -d {targetDB} -f {dump}.sql
On Import, the following error is thrown
psql:tbl_strainsLog.sql:1: ERROR: syntax error at or near "ÿ_" LINE 1: ÿ_-
^
While the error may seem intuitive, there is no such character anywhere in the file.
I had tried multiple different options for export and the import, all throw the same issue.
Encoding of source & target DB were set to UTF8, collation was en_US.utf8 on both as well.
I checked the initially loaded data, encoding was UTF8 there as well.
SETUP
Windows 10
Powershell
Postgres 13
Thanks, and I a m very curious if anyone has deeper insights as to what causes this issue in the first place.
So the culprit for the issue seems Powershell itself. While I have no exact knowledge of why this would happen, bypassing Powershell did the trick.
If I either run the above commands from cmd or I "pipe" thepg_dump command through cmd.exe, the error disappears and it works flawlessly.
So either...
A) in cmd run pg_dump -U {user} -d {sourceDB} -t {sourceTable} > {dump}.sql
or
B) in Powershell run & cmd.exe /c "pg_dump -U {user} -d {sourceDB} -t {sourceTable} > {dump}.sql"
This resolves the encoding issue created by pure Powershell during the export/dump.
The import works fine in Powershell, so the encoding issue only applies during export.
Hope this helps people struggeling with the same.

Using the inline '--command' argument with psql doesn't produce logs in .psql_history

When I log into my PostgreSQL server manually on Ubuntu and execute a command, I can then find it logged in /root/.psql_history.
However when I try to run a command in a bash script via psql -c "*query goes here*", the command returns data but is not logged in .psql_history.
Has anyone encountered this before?
Command line retrieval and editing, as well as the history file, are functions of the “readline” library that is linked to psql.
Readline support is only active in interactive sessions, so there is also no history written if you invoke psql with the -c or -f options.

Running PostgreSQL commands from anywhere on the terminal

First off, let me say that I'm new to both using Mac and PostgreSQL. I just installed Postgres using their installer and it was installed in /Library/Postgres/... when I tried running createdb from the terminal it returned an error createdb: command not found. I ended up using /library/postgresql/9.6/bin/createdb before I coud get it to work.
Here's my question, how do I set it so that I don't have to type in the full path again to use the createdb command.
I'd love a detailed explanation.
Thanks
First you need to execute the psql command to get into the postgresql interacive shell.
In your terminal:
psql
Postgresql interactive shell should start. In this shell
> createdb yourdatabasename;
Btw: If psql is not found you will probably need to add it to your path and restart your terminal, something like this with the path matching your machine:
export PATH=/Library/PostgreSQL/9.5/bin:$PATH

What does Usage: psql [wait] mean?

I'm trying to use psql version 9.6 on Mac OSX 10.11 and installed from the EnterpriseDB installer but I'm getting the following error:
MacBook-Pro:local me$ which psql
/usr/local/bin/psql
MacBook-Pro:local me$ /usr/local/bin/psql -h localhost; echo "Error code: $?"
Usage: /usr/local/bin/psql [wait]
Error code: 127
As you can see, I'm simply trying to connect to localhost and I'm getting a usage error despite following the syntax from the manual.
What is happening here?
Your bash is returning the error code 127: command not found.
See 127 Return code from $? for more details about this bash code.
Make sure that psql is in the directory you're trying to access.
Simple mistake - /usr/local/bin/psql was a symlink for /Library/PostgreSQL/9.6/scripts/runpsql.sh which doesn't accept any arguments... it's basically EnterpriseDB's shell script wrapper around the actual actual binary which is /Library/PostgreSQL/9.6/bin/psql.
I just needed to point psql to the real executable and not that shell script.

Deploy postgres via jenkins - continuous integration/deployment

I got my database dump (tables, functions, triggers etc) in *.sql files.
At this moment I am deploying them via jenkins, by passing execute shell command:
sudo -u postgres psql -d my_db < /[path_to_my_file].sql
The problem is, that if something is wrong in my sql file, build finishes as SUCCESS. I would like to got information immediately if something fails, without looking into log and checking if every command executed succesfully.
Is it possible (and how if the answer is 'yes') to deploy postgres database via jenkins other way?
I changed my execution command to:
sudo -u postgres psql -v ON_ERROR_STOP=1 -d my_db < [path_to_file].sql
Make sure you have set
set -e
Before running the command.
If that does not work, I'd look at the return code from the command above. That can be done by running
echo $?
right after the command.
If that gives you a zero when it fails it's postgres fault (sice it should return with something else than 0 on fail).
Perhaps there is a postgres flag to fail on wrong input.
EDIT:
-v ON_ERROR_STOP=1
As a flag to postgres should make postgres fail on errors