Unable to Automate PostgreSQL Login on Linux Server - linux

I've seen a few related questions here and tried pretty much all the solutions but there's obviously something silly I'm still missing. I'm trying to bypass the need to log in to PostgreSQL using a script. Its a pretty basic script to copy a text file on the server into a table in my database, and works fine except it prompts me to log in to PostgreSQL every time I run it. I intend to write a cron job that performs this task daily so the process needs to be automatic. Here's the script.
export PGPORT=5432
export PGDATABASE=db_name
export PGUSER=user
export PGPASSWORD=my_password
psql -h host_name -p 5432 -U user -W db_name -c "\COPY schema.table_name (col1, col2, col3, col4)
from path_to_txt_file with DELIMITER '^'"
I also went down the ".pgpass file" route to no avail. I saved it in /home/usr/.pgpass, and gave it the following credentials
*:*:*:user:my_password
saved it and then gave it permissions as follows
sudo chmod 600 .pgpass
I'm not sure if this is relevant but what I have as "usr" in my file path to the .pgpass file is different to my database username; what I have here as "user". Also the script I am running is in a completely different directory on the server to the .pgpass file. These are all novice points i'm sure but for the sake of being complete I thought I'd add them.
If there was a way to modify the existing script so that it didn't prompt me for a password that would be great, otherwise if anyone has any guidance on what I might be doing wrong with the .pgpass file I'd appreciate it.
Thanks in advance

I think the issue is option "-W" in the document of PostgreSQL, "-W" means "Force psql to prompt for a password before connecting to a database."
I suggest you to use this
export PGPORT=5432
export PGDATABASE=db_name
export PGUSER=user
export PGPASSWORD=my_password
psql -h host_name -p $PGPORT -U $PGUSER -d $PGDATABASE -c "\COPY schema.table_name(col1,
col2, col3, col4) from path_to_txt_file with DELIMITER '^'"

In addition to what Mabu said:
Postgresql has all it takes to automate logins and to be able to keep the connection parameters out of your code.
In addition to the .pgpass file for the password you can define all your connection parameters in a service file

I think the issue here is that my pg_hba.conf file needed to be edited to include the username that I am trying to access the database with. Unfortunately the database sits on an AWS RDS server instance, where the pg_hba.conf file is not editable. As well as manually going into the instance and trying to edit it without success, there is also mention of this here: https://serverfault.com/questions/560596/how-do-a-i-edit-conf-file-for-a-postgres-aws-rds. It will probably come down to building an EC2 instance where these configuration files are accessable.

I stand corrected with my answer above. The .pgpass file was just located in the wrong directory. I log into my linux server with the user name ubuntu, so after moving the .pgpass file into /home/ubuntu and deleting the PGPASSWORD line from the script above (the .pgpass file will be ignored if this is left in the script..) it now works perfectly. Looking at it now, it all seems quite obvious. I hope this might save someone a bit of stress in the future.

Related

PsExec - The file cannot be accessed by the system

I'm trying to execute a .bat File on a Server in a local network with psexec
I'm currently trying with this command:
.\PsExec.exe -i -u Administrator \\192.168.4.36 -s -d cmd.exe -c "Z:\NX_SystemSetup\test.bat"
The server has no password (it has no internet connection and is running a clean install of Windows Server 2016), so I'm currently not entering one, and when a password is asked I simply press enter, which seems to work. Also, the .bat File currently only opens notepad on execution.
When I enter this command, I get the message "The file cannot be acessed by the system"
I've tried executing it with powershell with administrator privileges (and also without, since I saw another user on Stackoverflow mention that it only worked for them that way) but to no success.
I'm guessing this is a privilege problem, since it "can't be accessed", which would indicate to me that the file was indeed found.
I used net share in a cmd and it says that C:\ on my server is shared.
The file I'm trying to copy is also not in any kind of restricted folder.
Any ideas what else I could try?
EDIT:
I have done a lot more troubleshooting.
On the Server, I went into the firewall settings and opened TCP Port 135 and 445 explicitly, since according to google, PsExec uses these.
Also on the Server, I opened Properties of the "windows" Folder in C: and added an admin$ share, where I gave everyone all rights to the folder (stupid ik but I'm desperate for this to work)
Also played around a bunch more with different commands. Not even .\PsExec.exe \\192.168.4.36 ipconfig seems to work. I still get the same error. "The file cannot be accessed by the system"
This is honestly maddening. There is no known documentation of this error on the internet. Searching explicitly for "File cannot be accessed" still only brings up results for "File cannot be found" and similar.
I'm surely just missing something obvious. Right?
EDIT 2
I also tried adding the domain name in front of the username. I checked the domain by using set user in cmd on the server.
.\PsExec.exe \\192.168.4.16 -u DomainName\Administrator -p ~ -c "C:\Users\UserName\Documents\Mellanox Update.bat"
-p ~
seems to work for the password, so I added that.
I also tried creating a shortcut of the .bat File, and executing it as Administrator, using it instead of the original .bat File. The error stays the same "The File cannot be accessed by the system"
As additional info, the PC I'm trying to send the command from has Windows 10, the Server is running Windows Server 2016
So, the reason for this specific error is as simple and as stupid as it gets.
Turns out I was using the wrong IP. The IP I was using is an IPMI Address, which does not allow for any traffic (other than IPMI related stuff)
I have not yet gotten it to work yet, since I've run into some different errors, but the original question/problem has been resolved.

Installing Postgresql and PgAdmin3 on Linux

I use Postgesql and PgAdmin on Windows without any problems but am struggling to get it working on Linux (I'm a Linux N00b). I have installed the 64bit version on my Fedora box following these instructions (except substituting version 9.3 for 9.2). Everything works well until I get to launching pgsql with:
sudo -u postgres psql template1
Here I get an error "Permission denied" as is attempts to change to my home directory.
I can change the password for 'postgres' as per the instructions and I presume this is just so I can use 'md5' rather than 'trust' in pg_hba.conf (if not, then I don't understand why I need this step).
When I fire up PgAdmin3, initially I have no connections. Attempting to connect to the server gives me an authentication error for user 'postgres'. I have tried resetting pg_hba.conf to 'trust' but that doesn't work either.
I would appreciate an explanation of the 'postgres' user in Linux as it appears to be a separate actual user on the Linux system (unlike Windows where it is just a 'disembodied' user relevant only to Postgress). Secondly, help in getting PgAdmin speaking to Postgresql would be gratefully received.
psql -U postgres template1
It will prompt for password if you set md5 in pg_hba.conf
After much digging about I found that most of the documentation is incomplete. I needed a combination of connecting via Cefull Lo's solution (+1) and NOT (as in most of the documentation):
sudo -U postgres psql
Then to change the password I need a more explicit SQL command as follows
template1=# ALTER USER postgres with encrypted password ‘yourpassword’;
I presume that I need to explicitly state 'with encrypted password' because I am using md5, though again, 90% of the documentation fails to point this out and advises a basic change of password together with md5. Maybe this is specific to Fedora, who knows, but now I can connect via PgAdmin3.

MySQL backup with CronJOb

I am trying to export MySQL data with CronJob in cPanel and I am adding bellow code into command line:
/usr/bin/mysqldump --opt -u krystald_fred -p'tY$645=&nm' max_joomla > /home/max/db-backup.sql
After CronJob runs and when I check db-backup.sql file I am getting blank file with no data inside .sql file.
What's wrong with this command line. Can anyone guide me to fix this.
Thanks.
Not enough info there. There could be many issues. Just run from command line with -v verbose flag and see. You might also need to connect via the 3306 port and specify hostname. It all depends on your installation.

Importing MySQL database from one server to another

I have got two dedicated servers with root access. Both are running Linux. I want to import database from Server1 to Server2. I have already created an empty database on Server2.
I want to know Linux command through which I can import database directly? Is there such a feature? Can I use mysqldump? I want to avoid first taking database backup on server1, then moving that file to server2, and then importing that file.
Can import be done directly using some command?
Thanks
If you want to avoid creating a file, transferring it, and loading it, you can just pipe mysqldump into either an mysql running on the other server, or an ssh mysql on the other server.
Using mysql to connect to the remote box:
mysqldump --all-databases | mysql -h yourserver.com
Using ssh to connect to the other server
mysqldump --all-databases | ssh user#yourserver.com mysql
Use the mysqldump --all-databases to transfer them all, or just specify database names. Refer to the mysqldump documentation for more options.
You can also use the MySQL "Replication" feature, although that will take a bit more time to setup, and is rather tricky. Probably not worth all the time and trouble just for one single migration.
Stop mysqld on the first server, copy the data directory (usually /var/lib/mysql) from server 1 to server 2, start mysqld on the second server, and it will now be identical to the first.
You do not have to use the import/export tools if you can stop the server while you copy the data files. Especially if you can compress the files before copying them, this will be the fastest way.
mysqldump –add-drop-table –extended-insert –force –log-error=error.log -uUSER -pPASS OLD_DB_NAME | ssh -C user#newhost “mysql -uUSER -pPASS NEW_DB_NAME”
Dumps a MySQL database over a compressed SSH tunnel and uses it as input to mysql
source

PostgreSQL without password prompt : .pgpass ignored

I'm trying to enable root (Ubuntu 8.04) to use psql command without password prompt (for scripting purpose). Everything worked fine with PostgreSQL 8.3, but I migrate to PostgreSQL 8.4 and the login without password doesn't work anymore.
I've a correct .pgpass file (the same used for 8.3), the right of /root/.pgpass are 0600 but calling psql keep asking for a password.
NB : the PGPASSFILE variable is empty, so I assume that the .pgpass file is supposed to be used.
Any brilliant idea?
Try to check if this works:
*:*:*:postgres:password_for_postgres_user
And then try:
VERBOSITY=verbose psql --no-password --user postgres
This question should probably be migrated to serverfault.com.

Resources