Connect with psql from remote machine to remote postgres database with saved password - postgresql

I am working on a remote Unix machine where I connect via SSH from my local machine. There is a Postgres database on another host that I need to connect from my remote machine using psql.
I would like to be able to run queries from an input file and save the results to an output file, however I did not manage to automatically set the password, surpassing the need to do it every time I run a query.
I tried the following:
Run
psql "user=username password=password host=hostname port=port dbname=database"`
with my username, password, hostname, port and database values.
This worked OK but I did not find a way to add arguments for input/output files (not needing to go through Postgres interactive environment).
According to another SO question, I created a file ~/.pgpass with the following format: hostname:port:database:username:password
and then chmod 0600 ~/.pgpass but when connecting as: psql -h hostname -U username -d database -w psql ignored this (I got the following failed authentication message.)
psql: fe_sendauth: no password supplied.
Note that my remote machine's name is different than my username, so this might also create a problem when creating the .pgpass file. Is there any other way to do it?
Any ideas?

This worked OK but I did not find a way to add arguments for
input/output files
You can either redirect the input file to psql, or specify it in -f option:
psql < input.sql
psql -f input.sql
As for output file, just redirect psql's output to it:
psql -f input.sql > output.txt

Related

Unable to execute postgres command

I'm having difficulty changing the password associated with the postgres user after installing postgres on my windows 10 machine. My apologies in advance as I'm quite unfamiliar with postgres as well as the commands required to work with it.
I've referenced the approved answer in the below article:
FATAL: password authentication failed for user "postgres" (postgresql 11 with pgAdmin 4)
I'm stuck on the step that requires me to
Connect using psql or pgAdmin4 or whatever you prefer
Run ALTER USER postgres PASSWORD 'fooBarEatsBarFoodBareFoot'
I don't quite understand this step. I've taken the following steps
Open cmd
run psql
The system then asks me for password for username jason. Regardless of what I enter, i get the following message:
psql: error: could not connect to server: FATAL: password authentication failed for user "jason"
At no point do I have an opportunity to enter the following command:
ALTER USER postgres PASSWORD 'fooBarEatsBarFoodBareFoot'
How can I run this command without being asked to enter a password for postgres?
Thanks!
The steps below require that you remember what you did when you installed PostgreSQL.
Locate the data directory where the installation process created the database cluster. By default, that would be a subdirectory of where you installed the software (which is a bad place)
Edit the pg_hba.conf file therein and add this line on top:
host postgres postgres 127.0.0.1/32 trust
Reload or restart PostgreSQL.
Start cmd.exe and enter
psql -h 127.0.0.1 -p 5432 -d postgres -U postgres
If psql is not on your PATH, use the absolute path C:\...\psql.
Use \password to change the password.

How to successfully restore a backup using pg_dump in postgresql?

I am new to Postrgresql. I want to create a backup of a production database and restore it in my development instance. Before I touch production, however, I want to make and restore a backup in development. I figured this would be a trivial effort, but that has not been the case.
I connected to psql using the command below.
sudo -u postgres psql
I ran the following command to create my backup inside psql.
\ pg_dump -U postgres -d dbname > /tmp/kp.bak
I included the "!" with the ""of the command above, but Stackoverflow is having trouble rendering that combination of characters
After that it prompted for a password. When I give it the correct password I get the following error. I reset the password for the PostgreSQL user using the ALTER PASSWORD command, so I know I have the correct password.
FATAL: password authentication failed for user "postgresql"
Since that doesn't work, I configured the pg_hba.conf to not require one and restarted the service. This has had no effect, as I am still prompted for a password when I try to restore. This is the first uncommented line in pg_hba.conf.
local all all trust
Here is the command I am using to do the restore.
\ pg_dump -U postgresql -h localhost -f \tmp\kp.bak dbname;
I am at a loss at how to move forward with this. Can anyone tell me what I am missing?
Thanks
Starting psql just to shell out to pg_dump doesn't make any sense. Just do it directly:
sudo -u postgres pg_dump ....
But once you have moved away from "peer" as your authentication method, there is no point in using the sudo at all, so just do it even more directly:
pg_dump ....
FATAL: password authentication failed for user "postgresql"
I don't find it believable that you specified -U postgres, yet the error message says user "postgresql". Please double check your post for spelling and typing errors.
I eventually got it to work with the following commands
sudo -U postgres psql
Then
\! pg_dump -U postgres -d dbname > /tmp/dbname.bak
Doing this outside of psql did not work. I received the following error despite running with sudo.
bash: /tmp/dbname.bak: Permission denied

starting postgresql and pgadmin in windows without installation

How can I start PostgreSQL and pgAdmin III in windows without installation. I do not have admin rights in a system. so I need to start the application without installing . How can I do that?
Download the ZIP file from https://www.enterprisedb.com/download-postgresql-binaries
Unzip the archive into a directory of your choice (the archive is created such that unzipping it, it will create a directory pgsql with everything else below that)
Run initdb (this can be found in the subdirectory pgsql\bin)
initdb -D c:\Users\Arthur\pgdata -U postgres -W -E UTF8 -A scram-sha-256
This will create the postgres "data directory" (aka the "cluster") in c:\Users\Arthur\pgdata. You need to make sure that the user running this command has full read/write privileges on that directory.
-U postgres creates the superuser as postgres, -W will prompt you for the password of the superuser, -E UTF8 will create the database with UTF-8 encoding and -A scram-sha-256 enables the password authentication.
To start Postgres, run:
pg_ctl -D c:\Users\Arthur\pgdata -l logfile start
this has(!) to be done as the user who ran initdb to avoid any problems with the access to the data directory.
To shutdown Postgres, run:
pg_ctl -D c:\Users\Arthur\pgdata stop
psql.exe (the command line client) is located in the bin directory. Starting with Postgres 9.6 the pgAdmin executable pgAdmin4.exe is located in the sub-directory "pgAdmin 4\bin".
Optionally create a Windows service to automatically run Postgres (must be run using a Windows administrator account)
pg_ctl register -N postgresql -D c:\Users\Arthur\pgdata
Thank you. This worked for me. But, i was getting error while starting psql.exe
The error was "psql: FATAL : role [user] does not exist."
To resolve this, i followed the below steps:
Be on the same folder path (where you have initdb.exe) i.e. source-folder/pgsql/bin
Run "psql -U postgres". This will ask for password.
Now enter the password that was set during postgres intialization. This will open the psql command prompt.
Hope this helps.. :)
I tried above method its working fine, but when i tried to connect it via JDBC i used to get
"The authentication type 10 is not supported. Check that you have configured the pg_hba.conf file to include the client's IP address or
subnet, and that it is using an authentication scheme supported by the
driver."
This happens because scram-sha-256 has some issue(refer here), i couldn't understand in depth, so i changed it to md5, All started to work smoothly.hope it helps
.\initdb -D D:\database\dbdata -U postgres -W -E UTF8 -A md5

How to run postgresql command from GCE shell?

Installing PostgreSQL on GCE requires root password to run sudo -u postgresql. This prompts for a password, which I was never given.
How do I get this pass, or any way to run postgresql commands from the shell in a different way?
Your system user postgresql doesn't have a password (I state with no proof... but I think you'll find this to be true.)
Normally you should use commands like these:
# Test that YOU can use psql (as postgres) to run a query:
psql -U postgres -c 'select * from pg_catalog.pg_user;'
# Test an interactive session:
psql -U postgres my_database
my_database=# select 42 as the_answer;
# Create a new database
psql -U postgres my_database
my_database=# create database mydb;
Alternatively, it's probably possible to login like this (it usually is):
sudo su postgres
And you could probably use this to run createdb. But running psql as yourself is probably better.
If you want to run commands against your postgresql server you should not need to use sudo, just use this syntax to enter the Postgresql Interactive Shell:
psql -U username database_name
OR
psql -U username hostname database_name
Replacing username with your postgresql usename, hostname (if not running on the same server) with the servers host name and database_name with the name of your database. For example:
psql -U postgressql customers
Normally sudo requires your user account password. So assuming that the account you are running the command from is listed in the sudoers file, the password it is prompting you for should be your own. Have you tried that, as opposed to the root or the postgresql password, which you don't appear to have (or might not even be set).

Can't I get to Postgres with plain psql

I always have to give the command like sudo -u postgres psql in order to login into Postgres console. What do I have to in order to login into postgres like sudo psql or psql
The environment I am working on is Ubuntu Linux 12.04
Thanks in advance.
It's normal that after the installation, only the postgres user is able to do anything with the database server. The installer can't assume that we'd want to open access to anyone else.
To give yourself access as a casual user, assuming as an example that your login name is joe (your normal, non-priviledged user), you just need to create a corresponding user and database:
Inside psql as the postgres administrator (with sudo -u postgres psql), issue:
CREATE USER joe;
CREATE DATABASE joe OWNER joe;
After that, when issuing psql at the shell prompt, it will connect by default to your own database with your username. You no longer have to sudo to postgres until you need to issue other administrator commands.
Your psql is in /usr/bin/psql. You shouldn't need to use sudo unless your permissions are wrong, or unless your link is wrong. (In later versions of PostgreSQL, /usr/bin/psql is a symbolic link to the executable. I don't know whether that's true in 8.4. On my home computer, it links to /usr/share/postgresql-common/pg_wrapper.)
The full skeleton syntax for psql is
psql -U username -h hostname -p portnumber database_name
So, for example, when I connect to my scratch database (named "sandbox"), I do it like this.
$ psql -U postgres -h localhost -p 5432 sandbox
You would substitute
your database username (which must already exist, and which isn't necessarily the same as your network/computer username),
your hostname (but "localhost" is probably right for a local install of PostgreSQL),
the port PostgreSQL is listening on (but 5432 is probably right; it's the default), and
your database name.
Would
psql -U psql
work for you?
EDIT:
I though you would mind about sudo.
If your problem is rather typing -U <user>, you could also set the environment variable PGUSER. This could also be done in your shell's logon script, so that it will always be set.
The other enviroment variables of interest might be PGDATABASE, PGHOST, PGPORT.