Having issues with command line and Postgres 12 psql - sql

I am a complete novice. I am following the freecodeacademy tutorial on Postgres and set up the environment fine with the bin and lib.
My issue comes with his next instructions which insinuate I can just put 'psql' and then it will open my environment to postgres. This is not the case for me. It actually prompts me to enter my password for my User profile as in my laptop userprofile and when I do, the password is marked as incorrect. for example, it's not showing C:\Program Files\PostgreSQL\12\bin> like I pathed it in the environment settings. Just my regular user profile.
The only way I have been able to enter the environment is using 'psql -U postgres' and then entering the password I set for it.
psql --help also doesn't work.
I was hoping anyone had any advice for me as a newbie (alternative resource/solution-wise)? I don't have cmd line or any coding experience so my troubleshooting with this is not good because it's very foreign to me.
Thanks for any help

You probably want to connect to the postgres database as user postgres:
psql -U postgres -d postgres
You can set the environment variables PGUSER and PGDATABASE to the user and database you want by default.

Related

Compare ms-sql database schemas on Ubuntu/Linux

Is there any command in ms-sql (on linux) to compare schemas between two databases?
I have very similar needs (I currently use PostgreSQL on Linux), and if doesn't have to necessarily be a ms-sql command I have 2 possible solutions:
Solution 1:
Use mssql-scripter from Microsoft (https://github.com/Microsoft/mssql-scripter)
You can get mssql-scripter via for example
pip install mssql-scripter.
and execute the following commands:
$ mssql-scripter -S serverName -d databaseSource -U user > ./source.sql
$ mssql-scripter -S serverName -d databaseTarget -U user > ./target.sql
$ diff source.sql target.sql
Solution 2:
If you have the possibility to use a desktop environment (as I'm doing) I would use a comparison tools, which is much more user friendly in my opinion.
TiCodeX SQL Schema Compare (https://www.ticodex.com) It's a nice tools that runs in Linux, Windows and Mac and can compare the schema of MS-SQL, MySQL and PostgreSQL database. Easy to use and effective. It may help you.
In order to use it:
Configure the source db (specifying servername, username, password, etc...)
Configure the target db
There are options in case you want to exclude database objects, or change the output
Press the comparison button
You will get the differences between the two databases, and eventually you also get the migration scripts to make the target db identical to the source.
It can perhaps can be done indirectly via
sqlpackage for Linux.
Firstly, dacpac of each database to be created:
sqlpackage.exe /Action:Extract /SourceServerName:XLW-CNU415CD8B /SourceDatabaseName:AdventureWorks2012 /TargetFile:AdventureWorks2012_v1.dacpac /p:IgnoreExtendedProperties=True /p:IgnorePermissions=False /p:ExtractApplicationScopedObjectsOnly=True
Then, dacpacs to be compared:
sqlpackage /a:DeployReport /sf:AdventureWorks2012_v1.dacpac /tf:AdventureWorks2012_v2.dacpac /tdn:AdventureWorks2012.db /op:AdventureWorks2012_v1.xml
Please note that this example is based on a windows version of the tool, I assume that Linux port has the same list of arguments

a .sql backup with pqsl won't work

sorry for my stupid question, but I need your help. I have tried everything, but nothing seems to work.
I want to restore a database using pgAdmin 4 and psql on windows 10. I have created a database and an user in pgAdmin 4. Then I open psql and execute set role to and then \i <name.sql>. I always get an error.
No such file or directory.
The file is in
psql C:/Users/hasan/a.sql
I have set a path in pgAdmin C:/Program Files/PostgresSQL/9.6/bin. I have also tried to restore the database with right-click on db in pgAdmin 4. It starts running and nothing happens.
It has been running for hours and hours. The process watcher also doesn't show any information. I don't know what to do. I have tried all solutions from this page and also watched youtube videos. It won't work.
Try
psql -f C:/Users/hasan/a.sql
or from inside psql
\i "C:/Users/hasan/a.sql"

Is there a better way than this to run an SQL script through puppet?

Take a look at Get puppet build to fail when the contained SQL script fails execution
I was attempting to run a vagrant build which installs Oracle XE in an Ubuntu Virtualbox VM and then runs a an SQL script to initialize the Oracle Schema. The vagrant build is here : https://github.com/ajorpheus/vagrant-ubuntu-oracle-xe. The setup.sql is run as a part of the oracle module's init.pp (right at the bottom or search for 'oracle-script').
When running the SQL script as a part of the vagrant build, I see the following error:
notice: /Stage[main]/Oracle::Xe/Exec[oracle-script]/returns: Error 6 initializing SQL*Plus
notice: /Stage[main]/Oracle::Xe/Exec[oracle-script]/returns: SP2-0667: Message file sp1<lang>.msb not found
notice: /Stage[main]/Oracle::Xe/Exec[oracle-script]/returns: SP2-0750: You may need to set ORACLE_HOME to your Oracle software directory
There were two things that were instrumental in me finding a workaround for the problem:
As suggested in this answer, setting the logoutput attribute to true for the exec block under question immediately showed me the error, whereas before the exec was just failing silently.
It seemed strange that I was able to run the command (sqlplus system/manager#xe < /tmp/setup.sql) after manually logging in as the 'vagrant' user. That suggested that there was something missing in the environment. Therefore, I copied all ORACLE env. vars into the exec as seen on Line 211 here
That worked, however, setting up the env vars manually seems a bit brittle. Is there a better way to setup the ORACLE environment for the vagrant user? Or, is there a way to get puppet to setup the environment for the vagrant user similar to an interactive shell?
If some profile has been set up to give the user a working interactive shell, you should be able to pass your action through such a shell
command => 'bash -i -c "<actual command>"'
As an aside about logoutput, since you mentioned that - the documentation advises that "on_failure" is a sane default, as it will only bloat your output when there are actual errors to analyze. It is the actual default in the latests versions of Puppet.

The local psql command could not be located

I'm following the instructions found here.
When I try to run $ heroku pg:psql or $ heroku pg:psql HEROKU POSTGRESQL_BROWN I recieve the following error message:
! The local psql command could not be located ! For help
installing psql, see local-postgresql
I can't find anything useful on the link it gives me (it just links to the instructions I was already using, but further down the page) nor can I find this error anywhere else.
If I've missed anything you need to know to answer this, just let me know. I'm rather new to all this and teaching myself as I go.
I had same error even after installing Postgres locally.
But after seeing this
I saw that "pqsl" was not in the PATH so I then did
PATH=%PATH%;C:\Program Files\PostgreSQL\9.2\bin
which worked for me
I have since solved this myself. When I ran heroku pg:info it says the version number is 9.1.8, I was locally running 9.2
installing 9.1.8 and ensuring Path pointed to the appropriate folder solved the problem.
After you change the path, make sure to restart the terminal!
Set the PATH. To find out the PATH of your psql script (on mac) open the sql shell script from your finder in Applications/Postgres installation. This will give you a hint as to where it is installed. That opened a window which told me it is located here: /Library/PostgreSQL/8.4/scripts/runpsql.sh
Then, I set the PATH variable from the terminal window by typing:
$ PATH="/Library/PostgreSQL/8.4/bin:$PATH"
(depends on the location of your PostgreSQL installation, find your bin path first, another exp: /usr/local/Cellar/postgresql#9.6/9.6.8/bin)
OR.....
You can also connect to the shell by opening the shell directly from your postgres installation folder. Then enter the credentials. If you don't know the credentials, here is how to find them out:
$ heroku pg:info
=== HEROKU_POSTGRESQL_RED_URL (DATABASE_URL)
$ heroku pg:credentials HEROKU_POSTGRESQL_RED_URL
Top answer wouldn't work for me oddly, my system would not add the Path via cmd with administrator access (Not sure why).
So check this > Windows key > environment variables > system variables
And add the last line (your version may differ in the path)
Make sure you've installed the toolbelt as psql is installed by default.
However you also need to ensure you've installed a local copy of PostgreSQL; if you don't the toolbelt will be unable to find the native psql client.
Assuming you have installed a local copy of PostgreSQL, make sure you can execute psql from the command line directly (i.e make sure you PATH is set correctly ). If the command does not execute, check your PATH, if it does execute see if you can connect via the PSQL connection string provided in the Heroku control panel. If you can connect reinstall the toolbelt, if you are unable to connect provision another dev database and try again.
If there are still issues, I would suggest contacting Heroku support for assistance after verifying no API issues are listed on the status page located here.
I got rid if this annoying message on Windows by adding a path element without the spaces, i.e.
C:\Progra~1\PostgreSQL\9.4\data
instead of
“C:\Program Files\PostgreSQL\9.4\data”
I followed the instructions here: http://www.computerhope.com/issues/ch000549.htm, which worked for me if you prefer to go the point-and-click configuration of the PATH variable.
This type of error usually appears in the Windows environment, because if you do not update the PATH after installing Postgresql, heroku pg:psql command does not work.
So you need to update your PATH environment variable to add the bin directory of your Postgres installation. The directory will look like this:
C:\Program Files\PostgreSQL\<VERSION>\bin.
For more information, go to the Heroku in Local setup website:
heroku-postgresql: Local setup
I had the same problem and discovered that Heroku doesn't seem to provision the latest version of PostgreSQL by default. Where the Heroku Getting Started instructions said
heroku addons:create heroku-postgresql:hobby-dev
That provisioned a v10 database for some reason (which you can check by clicking on Heroku Postgres in the Add-ons tab of your dashboard). I deleted that database and provisioned a new database using the --version flag:
heroku addons:create heroku-postgresql:hobby-dev --version 11
As of now, at least, you can find the latest version of Postgres supported by Heroku at this link: https://devcenter.heroku.com/articles/heroku-postgresql#version-support-and-legacy-infrastructure
I'm writing this in early 2019, but according to the PostgreSQL website the next version (12) is "tentatively scheduled" for third quarter of 2019 so if you're reading this in late 2019 potentially the same problem will come up for v12 instead
On Mac you can use the following:
export PATH="/Library/PostgreSQL/12/bin/:$PATH"
The only solution that I found on Windows:
go to advanced system settings
go to environment variables
select Path variable and click Edit
add a new line and enter your bin directory path (C:\Program Files\PostgreSQL<version>\bin) and click ok
restart your terminal
enter your psql command (heroku pg:psql)

T-SQL - Scripting variable not defined

I'm new to T-SQL and I'm trying to backup my databases (using SQL Server 2008).
When I try to run the script via sqlcmd -i inputfile I got this error messages:
'DATE' Scripting variable not defined.
The problem is I have a line like this:
...TO DISK = "FileName_$(ESCAPE_NONE(DATE)).BAK" ...
With a date in a filename, it will prevent it from replacing my old backups.
If I run it in management studio, it works, but if I run it in command line with the sqlcmd -i command, then it doesn't work.
EDIT:
I looked at the job history and I saw this error message:
"For SQL Server 2005 SP1 or later, you must use the appropriate ESCAPE_xxx
macro to update job steps containing tokens before the job can run"
I don't quite understand what that means. I've already used $ESCAPE_NONE(DATE), what's wrong?
Old question I know but this is one of the first results and if anyone else has the same problem the answer isn't particularly easy to find.
Including the -x switch to disable environment variables fixed the problem for me;
sqlcmd -x -i inputfile
If you're trying to backup your sql server databases and append the date to them using sqlcmd there's an easy thing you can try.
First, create the sp called sp_BackupDabases which you can find here:
http://support.microsoft.com/kb/2019698
You can invoke it from sql cmd using some command like this:
sqlcmd -U Damieh -P ilovechocolate -S (local) -Q "EXEC sp_BackupDatabases #backupLocation ='C:\MyBackups\', #BackupType='F'"
I'm sure you know this already, but just in case: -U is the user, -P is password, -S is server, and -Q is query. You can either backup all of your databases or some of them, there are parameters for that. You can find the stored proc parameters details on the same link I gave you.
The date will be automatically appended and you can play with the sp's code if you want it in a different place/way/format. I use this regularly on servers which don't have a non-express sqlserver (meaning that I can't schedule backups without using a .bat and task scheduler) with great success.
I apologize if this wasn't the answer you were looking for =). Have a nice day!
I know I'm coming along late on this thread, but you can use the following:
SQLCMD -S YourServer -E -d YourDatabase -i YourScript 2> nul
That will send the StdErrorOut to the bit bucket.